Python抓取京东图书评论数据


Posted in Python onAugust 31, 2014

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

from selenium import webdriver
from bs4 import BeautifulSoup
import re
import win32com.client
import threading,time
import MySQLdb

def mydebug():
    driver.quit()
    exit(0)

def catchDate(s):
    """页面数据提取"""
    soup = BeautifulSoup(s)
    z = []
    global nowtimes
   
    m = soup.findAll("div",class_="date-buy")
    for obj in m:
        try:
            tmp = obj.find('br').contents
        except Exception, e:
            continue
        if(tmp != ""):
            z.append(tmp)
            nowtimes += 1
    return z

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

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

#断点续抓
num1 = input("bookid:")
num2 = input("pagenumber:")

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

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

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

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

flag = 0
flag2 = 0

# 如果有数据返回就循环输出,http://club.jd.com/review/10178500-1-154.html
if alldata:
    for rec in alldata:
        #rec[0]--bookid,rec[1]--cateid,rec[2]--pagenumber
        if(rec[0] != str(num1) and flag == 0):
            continue
        else:
            flag = 1
        for p in range(num2,rec[2]):
            if(flag2 == 0):
                num2 = 0
                flag2 = 1
            p += 1
            link = "http://club.jd.com/review/" + rec[0] + "-1-" + str(p) + ".html"
            #抓网页
            driver.get(link)
            html = driver.page_source
            #抓评论
            buydate = catchDate(html)
            #写入数据库
            for z in buydate:
                sql = "INSERT INTO ljj (id, cateid, bookid, date) VALUES (NULL, '" + rec[0] + "','" + rec[1] + "','" + z[0] + "');"
                try:
                    cursor.execute(sql)
                except Exception, e:
                    print e
            conn.commit()
        print getTimes(nowtimes,totaltimes)

driver.quit()
cursor.close()
conn.close()

Python 相关文章推荐
Python中的Classes和Metaclasses详解
Apr 02 Python
Python扫描IP段查看指定端口是否开放的方法
Jun 09 Python
编写Python爬虫抓取暴走漫画上gif图片的实例分享
Apr 20 Python
Python中import导入上一级目录模块及循环import问题的解决
Jun 04 Python
python图像常规操作
Nov 11 Python
python实现图书管理系统
Mar 12 Python
python write无法写入文件的解决方法
Jan 23 Python
Python将列表数据写入文件(txt, csv,excel)
Apr 03 Python
python 3.7.4 安装 opencv的教程
Oct 10 Python
python matplotlib 画dataframe的时间序列图实例
Nov 20 Python
基于python实现复制文件并重命名
Sep 16 Python
python图片灰度化处理的几种方法
Jun 23 Python
Python深入学习之内存管理
Aug 31 #Python
Python深入学习之装饰器
Aug 31 #Python
Python深入学习之闭包
Aug 31 #Python
Python深入学习之对象的属性
Aug 31 #Python
Python深入学习之上下文管理器
Aug 31 #Python
Python深入学习之特殊方法与多范式
Aug 31 #Python
python中的reduce内建函数使用方法指南
Aug 31 #Python
You might like
B2K与车机的中波PK
2021/03/02 无线电
构建简单的Webmail系统
2006/10/09 PHP
奇怪的PHP引用效率问题分析
2012/03/23 PHP
在视频前插入广告
2006/11/20 Javascript
基于jquery的二级联动菜单实现代码
2011/04/25 Javascript
NodeJS 模块开发及发布详解分享
2012/03/07 NodeJs
window.open的页面如何刷新(父页面)上层页面
2012/12/28 Javascript
js设置控件的隐藏与显示的两种方法
2014/08/21 Javascript
控制台报错object is not a function的解决方法
2014/08/24 Javascript
js随机生成网页背景颜色的方法
2015/02/26 Javascript
JavaScript中cookie工具函数封装的示例代码
2016/10/11 Javascript
webpack使用 babel-loader 转换 ES6代码示例
2017/08/21 Javascript
一文让你彻底搞清楚javascript中的require、import与export
2017/09/24 Javascript
解决element-ui中下拉菜单子选项click事件不触发的问题
2018/08/22 Javascript
vue-router 手势滑动触发返回功能
2018/09/30 Javascript
[49:27]LGD vs OG 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
PyQt实现界面翻转切换效果
2018/04/20 Python
numpy实现合并多维矩阵、list的扩展方法
2018/05/08 Python
基于python的selenium两种文件上传操作实现详解
2019/09/19 Python
Python 异常的捕获、异常的传递与主动抛出异常操作示例
2019/09/23 Python
如何使用Python多线程测试并发漏洞
2019/12/18 Python
安装并免费使用Pycharm专业版(学生/教师)
2020/09/24 Python
Pycharm 跳转回之前所在页面的操作
2021/02/05 Python
利用HTML5中Geolocation获取地理位置调用Google Map API在Google Map上定位
2013/01/23 HTML / CSS
澳大利亚第一的设计师礼服租赁网站:GlamCorner
2017/08/13 全球购物
科颜氏印度官网:Kiehl’s印度
2021/02/20 全球购物
学生党员思想汇报
2013/12/28 职场文书
后进生转化工作制度
2014/01/17 职场文书
社区工作者先进事迹
2014/01/18 职场文书
2014年公务员思想汇报范文:全心全意为人民服务
2014/03/06 职场文书
上课睡觉万能检讨书
2015/02/17 职场文书
聘任证明怎么写
2015/03/02 职场文书
毕业生个人自荐书
2015/03/05 职场文书
行政处罚告知书
2015/07/01 职场文书
委托书范本格式
2019/04/18 职场文书
Java线程的6种状态与生命周期
2022/05/11 Java/Android