Python抓取京东图书评论数据

yipeiwu_com5年前Python爬虫

 京东图书评论有非常丰富的信息,这里面就包含了购买日期、书名、作者、好评、中评、差评等等。以购买日期为例,使用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实现抓取网页并解析的功能。主要解析问答与百度的首页。分享给大家供大家参考之用。 主要功能代码如下: #!/usr/bin/python #coding...

编写Python脚本抓取网络小说来制作自己的阅读器

编写Python脚本抓取网络小说来制作自己的阅读器

你是否苦恼于网上无法下载的“小说在线阅读”内容?或是某些文章的内容让你很有收藏的冲动,却找不到一个下载的链接?是不是有种自己写个程序把全部搞定的冲动?是不是学了 python,想要找点东...

使用Python抓取豆瓣影评数据的方法

使用Python抓取豆瓣影评数据的方法

抓取豆瓣影评评分 正常的抓取 分析请求的url https://movie.douban.com/subject/26322642/comments?start=20&limit=...

python爬虫入门教程--正则表达式完全指南(五)

python爬虫入门教程--正则表达式完全指南(五)

前言 正则表达式处理文本有如疾风扫秋叶,绝大部分编程语言都内置支持正则表达式,它应用在诸如表单验证、文本提取、替换等场景。爬虫系统更是离不开正则表达式,用好正则表达式往往能收到事半功倍的...

Python爬虫实现抓取京东店铺信息及下载图片功能示例

本文实例讲述了Python爬虫实现抓取京东店铺信息及下载图片功能。分享给大家供大家参考,具体如下: 这个是抓取信息的 from bs4 import BeautifulSoup im...