Python抓取京东图书评论数据
admin
2023-07-31 02:04:25
0

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

相关内容

热门资讯

Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...
python清除字符串里非数字... 本文实例讲述了python清除字符串里非数字字符的方法。分享给大家供大家参考。具体如下: impor...