python爬取亞馬遜書籍信息代碼分享
我有個(gè)需求就是抓取一些簡(jiǎn)單的書籍信息存儲(chǔ)到mysql數(shù)據(jù)庫(kù),例如,封面圖片,書名,類型,作者,簡(jiǎn)歷,出版社,語(yǔ)種。
我比較之后,決定在亞馬遜來(lái)實(shí)現(xiàn)我的需求。
我分析網(wǎng)站后發(fā)現(xiàn),亞馬遜有個(gè)高級(jí)搜索的功能,我就通過(guò)該搜索結(jié)果來(lái)獲取書籍的詳情URL。
由于亞馬遜的高級(jí)搜索是用get方法的,所以通過(guò)分析,搜索結(jié)果的URL,可得到node參數(shù)是代表書籍類型的。field-binding_browse-bin是代表書籍裝飾。
所以我固定了書籍裝飾為平裝,而書籍的類型,只能每次運(yùn)行的時(shí)候,爬取一種類型的書籍難過(guò)
之后就是根據(jù)書籍詳情頁(yè)面利用正則表達(dá)式來(lái)匹配需要的信息了。
以下源代碼,命名不是很規(guī)范。。。
import requests
import sys
import re
import pymysql
class product:
type="歷史"
name=""
author=""
desciption=""
pic1=""
languages=""
press=""
def getProUrl():
urlList = []
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"}
session = requests.Session()
furl="https://www.amazon.cn/gp/search/ref=sr_adv_b/?search-alias=stripbooks&field-binding_browse-bin=2038564051&sort=relevancerank&page="
for i in range(1,7):
html=""
print(furl+str(i))
html = session.post(furl+str(i)+'&node=658418051',headers = headers)
html.encoding = 'utf-8'
s=html.text.encode('gb2312','ignore').decode('gb2312')
url=r'</li><li id=".*?" data-asin="(.+?)" class="s-result-item celwidget">'
reg=re.compile(url,re.M)
items = reg.findall(html.text)
for i in range(0,len(items)):
urlList.append(items[i])
urlList=set(urlList)
return urlList
def getProData(url):
pro = product()
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"}
session = requests.Session()
zurl="https://www.amazon.cn/dp/"
html = session.get(zurl+url,headers = headers)
html.encoding = 'utf-8'
s=html.text.encode('gb2312','ignore').decode('gb2312')
pro.pic1=getProPic(html)
pro.name=getProName(html)
pro.author=getProAuthor(html)
pro.desciption=getProDescrip(html)
pro.press=getProPress(html)
pro.languages=getProLanguages(html)
return pro
def getProPic(html):
pic=r'id="imgBlkFront" data-a-dynamic-image="{"(.+?)".*?}"'
reg=re.compile(pic,re.M)
items = reg.findall(html.text)
if len(items)==0:
return ""
else:
return items[0]
def getProName(html):
name=r'<div class="ma-title"><p class="wraptext goto-top">(.+?)<span'
reg=re.compile(name,re.M)
items = reg.findall(html.text)
if len(items)==0:
return ""
else:
return items[0]
def getProAuthor(html):
author=r'<span class="author.{0,20}" data-width="".{0,30}>.*?<a class="a-link-normal" href=".*?books" rel="external nofollow" >(.+?)</a>.*?<span class="a-color-secondary">(.+?)</span>'
reg=re.compile(author,re.S)
items = reg.findall(html.text)
au=""
for i in range(0,len(items)):
au=au+items[i][0]+items[i][1]
return au
def getProDescrip(html):
Descrip=r'<noscript>.{0,30}<div>(.+?)</div>.{0,30}<em></em>.{0,30}</noscript>.{0,30}<div id="outer_postBodyPS"'
reg=re.compile(Descrip,re.S)
items = reg.findall(html.text)
if len(items)==0:
return ""
else:
position = items[0].find('海報(bào):')
descrip=items[0]
if position != -1:
descrip=items[0][0:position]
return descrip.strip()
def getProPress(html):
press=r'<li><b>出版社:</b>(.+?)</li>'
reg=re.compile(press,re.M)
items = reg.findall(html.text)
if len(items)==0:
return ""
else:
return items[0].strip()
def getProLanguages(html):
languages=r'<li><b>語(yǔ)種:</b>(.+?)</li>'
reg=re.compile(languages,re.M)
items = reg.findall(html.text)
if len(items)==0:
return ""
else:
return items[0].strip()
def getConnection():
config = {
'host':'121.**.**.**',
'port':3306,
'user':'root',
'password':'******',
'db':'home_work',
'charset':'utf8',
'cursorclass':pymysql.cursors.DictCursor,
}
connection = pymysql.connect(**config)
return connection
urlList = getProUrl()
i = 0
for d in urlList:
i = i + 1
print (i)
connection = getConnection()
pro = getProData(d)
try:
with connection.cursor() as cursor:
sql='INSERT INTO books (type,name,author,desciption,pic1,languages,press) VALUES (%s,%s,%s,%s,%s,%s,%s)'
cursor.execute(sql,(pro.type,pro.name,pro.author,pro.desciption,pro.pic1,pro.languages,pro.press))
connection.commit()
finally:
connection.close();
總結(jié)
以上就是本文關(guān)于python爬取亞馬遜書籍信息代碼分享的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
matplotlib在python上繪制3D散點(diǎn)圖實(shí)例詳解
python的unittest測(cè)試類代碼實(shí)例
Python編程實(shí)現(xiàn)使用線性回歸預(yù)測(cè)數(shù)據(jù)
如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
使用Python操作MySQL數(shù)據(jù)庫(kù)的教程詳解
在這篇文章中,主要為大家詳細(xì)介紹如何在Python中使用pymysql模塊來(lái)操作MySQL數(shù)據(jù)庫(kù),文中的示例代碼簡(jiǎn)介易懂,需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-07-07
python開啟攝像頭以及深度學(xué)習(xí)實(shí)現(xiàn)目標(biāo)檢測(cè)方法
今天小編就為大家分享一篇python開啟攝像頭以及深度學(xué)習(xí)實(shí)現(xiàn)目標(biāo)檢測(cè)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Python操作word實(shí)現(xiàn)添加文字或圖片水印
這篇文章主要為大家詳細(xì)介紹了如何使用Spire.Doc for Python在程序中的輕松添加文字和圖像水印到Word文檔,感興趣的小伙伴可以跟隨小編一起了解一下2023-10-10
python?pandas?數(shù)據(jù)排序的幾種常用方法
這篇文章主要介紹了python?pandas數(shù)據(jù)排序的幾種常用方法,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
python利用while求100內(nèi)的整數(shù)和方式
這篇文章主要介紹了 python利用while求100內(nèi)的整數(shù)和方式,下面文章要描述的內(nèi)容有1到100的和、1到100內(nèi)的偶數(shù)和、1到100內(nèi)的奇數(shù)和,具體詳細(xì)內(nèi)容,需要的朋友可以參考一下2021-11-11
python使用pandas實(shí)現(xiàn)篩選功能方式
在數(shù)據(jù)分析的過(guò)程中通常要對(duì)數(shù)據(jù)進(jìn)行清洗與處理,而其中比較重要和常見的操作就有對(duì)數(shù)據(jù)進(jìn)行篩選與查詢,下面這篇文章主要給大家介紹了關(guān)于python使用pandas實(shí)現(xiàn)篩選功能方式的相關(guān)資料,需要的朋友可以參考下2022-06-06
Python 安裝 virturalenv 虛擬環(huán)境的教程詳解
這篇文章主要介紹了Python 安裝 virturalenv 虛擬環(huán)境的教程,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
使用Python的SymPy庫(kù)解決數(shù)學(xué)運(yùn)算問(wèn)題的方法
這篇文章主要介紹了使用Python的SymPy庫(kù)解決數(shù)學(xué)運(yùn)算問(wèn)題的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03
PyMongo 查詢數(shù)據(jù)的實(shí)現(xiàn)
本文主要介紹了PyMongo 查詢數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06

