Chinaunix首页 | 论坛 | 博客
  • 博客访问: 967339
  • 博文数量: 403
  • 博客积分: 27
  • 博客等级: 民兵
  • 技术积分: 165
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-25 22:20
文章分类

全部博文(403)

文章存档

2016年(3)

2015年(16)

2014年(163)

2013年(222)

分类: Python/Ruby

2014-01-15 11:31:30


   京东图书评论有非常丰富的信息,这里面就包含了购买日期、书名、作者、好评、中评、差评等等。以购买日期为例,使用Python + Mysql的搭配进行实现,程序不大,才100行。相关的解释我都在程序里加注了:

  1. from selenium import webdriver
  2. from bs4 import BeautifulSoup
  3. import re
  4. import win32com.client
  5. import threading,time
  6. import MySQLdb

  7. def mydebug():
  8.     driver.quit()
  9.     exit(0)

  10. def catchDate(s):
  11.     """页面数据提取"""
  12.     soup = BeautifulSoup(s)
  13.     z = []
  14.     global nowtimes
  15.     
  16.     m = soup.findAll("div",class_="date-buy")
  17.     for obj in m:
  18.         try:
  19.             tmp = obj.find('br').contents
  20.         except Exception, e:
  21.             continue
  22.         if(tmp != ""):
  23.             z.append(tmp)
  24.             nowtimes += 1
  25.     return z

  26. def getTimes(n,t):
  27.     """获取当前进度"""
  28.     return "当前进度为:" + str(int(100*n/t)) + "%"


  29. #———————————————————————————————————| 程序开始 |—————————————————————————————————
  30. #确定图书大类
  31. cate = {"3273":"历史","3279":"心理学","3276":"政治军事","3275":"国学古籍","3274":"哲学宗教","3277":"法律","3280":"文化","3281":"社会科学"}

  32. #断点续抓
  33. num1 = input("bookid:")
  34. num2 = input("pagenumber:")

  35. #生成图书大类链接,共需17355*20 = 347100次
  36. totaltimes = 347100.0
  37. nowtimes = 0

  38. #开启webdirver的PhantomJS对象
  39. #driver = webdriver.PhantomJS()
  40. driver = webdriver.Ie('C:\Python27\Scripts\IEDriverServer')
  41. #driver = webdriver.Chrome('C:\Python27\Scripts\chromedriver')

  42. #读出Mysql中的评论页面,进行抓取
  43. # 连接数据库 
  44. try:
  45.     conn = MySQLdb.connect(host='localhost',user='root',passwd='',db='jd')
  46. except Exception, e:
  47.     print e
  48.     sys.exit()

  49. # 获取cursor对象
  50. cursor = conn.cursor()
  51. sql = "SELECT * FROM booknew ORDER BY pagenumber DESC"
  52. cursor.execute(sql)
  53. alldata = cursor.fetchall()

  54. flag = 0
  55. flag2 = 0

  56. # 如果有数据返回就循环输出,http://club.jd.com/review/10178500-1-154.html
  57. if alldata:
  58.     for rec in alldata:
  59.         #rec[0]--bookid,rec[1]--cateid,rec[2]--pagenumber
  60.         if(rec[0] != str(num1) and flag == 0):
  61.             continue
  62.         else:
  63.             flag = 1
  64.         for p in range(num2,rec[2]):
  65.             if(flag2 == 0):
  66.                 num2 = 0
  67.                 flag2 = 1
  68.             p += 1
  69.             link = "" + rec[0] + "-1-" + str(p) + ".html"
  70.             #抓网页
  71.             driver.get(link)
  72.             html = driver.page_source
  73.             #抓评论
  74.             buydate = catchDate(html)
  75.             #写入数据库
  76.             for z in buydate:
  77.                 sql = "INSERT INTO ljj (id, cateid, bookid, date) VALUES (NULL, '" + rec[0] + "','" + rec[1] + "','" + z[0] + "');"
  78.                 try:
  79.                     cursor.execute(sql)
  80.                 except Exception, e:
  81.                     print e
  82.             conn.commit()
  83.         print getTimes(nowtimes,totaltimes)

  84. driver.quit()
  85. cursor.close()
  86. conn.close()



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