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):
"""獲取當(dāng)前進度"""
return "當(dāng)前進度為:" + str(int(100*n/t)) + "%"
#———————————————————————————————————| 程序開始 |—————————————————————————————————
#確定圖書大類
cate = {"3273":"歷史","3279":"心理學(xué)","3276":"政治軍事","3275":"國學(xué)古籍","3274":"哲學(xué)宗教","3277":"法律","3280":"文化","3281":"社會科學(xué)"}
#斷點續(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)文章
Python 面向?qū)ο笾恈lass和對象基本用法示例
這篇文章主要介紹了Python 面向?qū)ο笾恈lass和對象基本用法,結(jié)合實例形式詳細分析了Python面向?qū)ο蟪绦蛟O(shè)計中類class和對象基本概念、原理、使用方法與操作注意事項,需要的朋友可以參考下2020-02-02
python中os.path.dirname(path)詳細解釋和使用示例
這篇文章主要介紹了python中os.path.dirname(path)詳細解釋和使用示例,os.path.dirname是一個Python函數(shù),用于獲取文件路徑的目錄部分,它通常與os.path.basename結(jié)合使用,以分離路徑中的目錄和文件名,需要的朋友可以參考下2025-03-03
利用Python計算質(zhì)數(shù)與完全數(shù)的方法實例
這篇文章主要介紹了利用Python計算質(zhì)數(shù)與完全數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Python實現(xiàn)Excel和TXT文本之間相互轉(zhuǎn)換
Excel是一種具有強大的數(shù)據(jù)處理和圖表制作功能的電子表格文件,而TXT則是一種簡單通用、易于編輯的純文本文件,本文將介紹如何使用Python并結(jié)合相關(guān)庫來實現(xiàn) Excel 和 TXT 文本文件之間的相互轉(zhuǎn)換,需要的朋友可以參考下2024-06-06

