国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python爬取亞馬遜書籍信息代碼分享

2020-02-16 11:01:38
字體:
供稿:網(wǎng)友

我有個(gè)需求就是抓取一些簡單的書籍信息存儲(chǔ)到mysql數(shù)據(jù)庫,例如,封面圖片,書名,類型,作者,簡歷,出版社,語種。

我比較之后,決定在亞馬遜來實(shí)現(xiàn)我的需求。

我分析網(wǎng)站后發(fā)現(xiàn),亞馬遜有個(gè)高級(jí)搜索的功能,我就通過該搜索結(jié)果來獲取書籍的詳情URL。

由于亞馬遜的高級(jí)搜索是用get方法的,所以通過分析,搜索結(jié)果的URL,可得到node參數(shù)是代表書籍類型的。field-binding_browse-bin是代表書籍裝飾。

所以我固定了書籍裝飾為平裝,而書籍的類型,只能每次運(yùn)行的時(shí)候,爬取一種類型的書籍難過

之后就是根據(jù)書籍詳情頁面利用正則表達(dá)式來匹配需要的信息了。

以下源代碼,命名不是很規(guī)范。。。

import requestsimport sysimport reimport pymysqlclass 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 urlListdef 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 prodef 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 audef 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>語種:</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 connectionurlList = getProUrl()i = 0for 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();            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 六盘水市| 松原市| 于田县| 云梦县| 荃湾区| 固原市| 诸城市| 祁门县| 民和| 建昌县| 张家川| 莫力| 枣庄市| 盐山县| 西乌珠穆沁旗| 湾仔区| 余江县| 石狮市| 蒙阴县| 竹山县| 体育| 吉木萨尔县| 肇庆市| 万安县| 开江县| 新津县| 洛隆县| 海口市| 永泰县| 苍梧县| 方山县| 凌云县| 布拖县| 亚东县| 灌云县| 江山市| 枝江市| 化德县| 华蓥市| 兰考县| 望都县|