Chinaunix首页 | 论坛 | 博客
  • 博客访问: 23019
  • 博文数量: 18
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 190
  • 用 户 组: 普通用户
  • 注册时间: 2021-06-07 17:12
个人简介

对嵌入式、QT、桌面开发感兴趣

文章分类
文章存档

2023年(2)

2022年(16)

我的朋友

分类: Python/Ruby

2022-09-13 12:00:00


  1. import requests
  2. import re
  3. import sqlite3

  4. con = sqlite3.connect("data.db")
  5. cur = con.cursor()

  6. sql = """CREATE TABLE IF NOT EXISTS titles (
  7.             id INTEGER PRIMARY KEY AUTOINCREMENT,
  8.             title TEXT,
  9.             add_date TEXT)"""
  10. cur.execute(sql)

  11. sql = """CREATE TABLE IF NOT EXISTS viewed (
  12.             title_id INTEGER,
  13.             check_date TEXT,
  14.             view INTEGER,
  15.             comm INTEGER,
  16.             forward INTEGER)"""
  17. cur.execute(sql)
  18. con.commit()

  19. url = 'http://blog.chinaunix.net/uid/70001461/abstract/1.html'
  20. response = requests.get(url)

  21. reg = re.compile(r"()", re.S)
  22. table = reg.findall(response.text)

  23. reg = re.compile(r"()", re.S)
  24. trs = reg.findall(table[0])

  25. for tr in trs:
  26.     reg = re.compile(r"html\">(.*?)", re.S)
  27.     title = reg.findall(tr)
  28.     if len(title) == 0:
  29.         continue

  30.     reg = re.compile(r"(.*?)", re.S)
  31.     vals = reg.findall(tr)

  32.     sql = "select * from titles where title = '{0}' and add_date='{1}'".format(title[0], vals[3])
  33.     cur.execute(sql)
  34.     data = cur.fetchall()
  35.     if len(data) == 0:
  36.         sql = "insert into titles (title, add_date) values ('{0}', '{1}')".format(title[0], vals[3])
  37.         cur.execute(sql)
  38.         con.commit()
  39.         sql = "select * from titles where title = '{0}' and add_date='{1}'".format(title[0], vals[3])
  40.         cur.execute(sql)
  41.         data = cur.fetchall()
  42.     sql = "insert into viewed (title_id, check_date, view, comm, forward) values ({0}, datetime('now','localtime'), '{1}', {2}, {3})".format(data[0][0], vals[0],vals[1],vals[2])
  43.     cur.execute(sql)
  44.     con.commit()

阅读(346) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~