Python抓取京東圖書評論數(shù)據(jù)
京東圖書評論有非常豐富的信息,這里面就包含了購買日期、書名、作者、好評、中評、差評等等。以購買日期為例,使用Python + Mysql的搭配進行實現(xiàn),程序不大,才100行。相關(guān)的解釋我都在程序里加注了:
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):
"""頁面數(shù)據(jù)提取"""
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":"社會科學"}
#斷點續(xù)抓
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中的評論頁面,進行抓取
# 連接數(shù)據(jù)庫
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
# 如果有數(shù)據(jù)返回就循環(huán)輸出,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 = "
#抓網(wǎng)頁
driver.get(link)
html = driver.page_source
#抓評論
buydate = catchDate(html)
#寫入數(shù)據(jù)庫
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采用requests庫模擬登錄和抓取數(shù)據(jù)的簡單示例
- Python爬蟲抓取手機APP的傳輸數(shù)據(jù)
- 通過抓取淘寶評論為例講解Python爬取ajax動態(tài)生成的數(shù)據(jù)(經(jīng)典)
- python抓取某汽車網(wǎng)數(shù)據(jù)解析html存入excel示例
- Python實現(xiàn)并行抓取整站40萬條房價數(shù)據(jù)(可更換抓取城市)
- Python基于多線程實現(xiàn)抓取數(shù)據(jù)存入數(shù)據(jù)庫的方法
- 對python抓取需要登錄網(wǎng)站數(shù)據(jù)的方法詳解
- 在Python3中使用asyncio庫進行快速數(shù)據(jù)抓取的教程
- 使用Python抓取豆瓣影評數(shù)據(jù)的方法
- python數(shù)據(jù)抓取3種方法總結(jié)
相關(guān)文章
Pandas:Series和DataFrame刪除指定軸上數(shù)據(jù)的方法
今天小編就為大家分享一篇Pandas:Series和DataFrame刪除指定軸上數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11PyCharm 無法 import pandas 程序卡住的解決方式
這篇文章主要介紹了PyCharm 無法 import pandas 程序卡住的解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Django項目中包含多個應(yīng)用時對url的配置方法
今天小編就為大家分享一篇Django項目中包含多個應(yīng)用時對url的配置方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05使用PyWebCopy在Python中克隆網(wǎng)頁的操作方法
PyWebCopy是一個用于克隆網(wǎng)頁內(nèi)容的Python庫,它允許用戶從指定的 URL 復(fù)制整個網(wǎng)頁并保存到本地,本文將介紹 PyWebCopy 的基本用法,以及如何克隆網(wǎng)頁并保存網(wǎng)頁內(nèi)容到本地文件夾,文中通過代碼示例講解的非常詳細,需要的朋友可以參考下2023-12-12python模擬點擊網(wǎng)頁按鈕實現(xiàn)方法
在本篇文章里小編給大家整理的是一篇關(guān)于python模擬點擊網(wǎng)頁按鈕實現(xiàn)方法,需要的朋友們可以參考下。2020-02-02