Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4052222
  • 博文数量: 272
  • 博客积分: 7846
  • 博客等级: 少将
  • 技术积分: 6476
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-25 16:27
文章分类

全部博文(272)

分类: Python/Ruby

2014-01-14 20:34:56


   京东图书评论有非常丰富的信息,这里面就包含了购买日期、书名、作者、好评、中评、差评等等。以购买日期为例,使用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()



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

五岳之巅2014-02-13 09:52:57

pikaqiu_chao:楼主,能不能把整个项目放上来啊。。。初学者想学习一下,而且现在急需京东商品评论数据。。。。。。

已经是我完整的抓取程序了,就这么大。当然后台是mysql。你可以在“页面数据提取”中多抓一些你的目标dom tree。

回复 | 举报

pikaqiu_chao2014-02-12 17:47:46

楼主,能不能把整个项目放上来啊。。。初学者想学习一下,而且现在急需京东商品评论数据。。。。。。