python爬取亚马逊书籍信息代码分享

yipeiwu_com4年前Python爬虫

我有个需求就是抓取一些简单的书籍信息存储到mysql数据库,例如,封面图片,书名,类型,作者,简历,出版社,语种。

我比较之后,决定在亚马逊来实现我的需求。

我分析网站后发现,亚马逊有个高级搜索的功能,我就通过该搜索结果来获取书籍的详情URL。

由于亚马逊的高级搜索是用get方法的,所以通过分析,搜索结果的URL,可得到node参数是代表书籍类型的。field-binding_browse-bin是代表书籍装饰。

所以我固定了书籍装饰为平装,而书籍的类型,只能每次运行的时候,爬取一种类型的书籍难过

之后就是根据书籍详情页面利用正则表达式来匹配需要的信息了。

以下源代码,命名不是很规范。。。

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('海报:')
    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 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();

总结

以上就是本文关于python爬取亚马逊书籍信息代码分享的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:

matplotlib在python上绘制3D散点图实例详解

python的unittest测试类代码实例

Python编程实现使用线性回归预测数据

如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

python编写爬虫小程序

起因 深夜忽然想下载一点电子书来扩充一下kindle,就想起来python学得太浅,什么“装饰器”啊、“多线程”啊都没有学到。 想到廖雪峰大神的python教程很经典、很著名。就想找找有...

python爬虫入门教程--正则表达式完全指南(五)

python爬虫入门教程--正则表达式完全指南(五)

前言 正则表达式处理文本有如疾风扫秋叶,绝大部分编程语言都内置支持正则表达式,它应用在诸如表单验证、文本提取、替换等场景。爬虫系统更是离不开正则表达式,用好正则表达式往往能收到事半功倍的...

Python爬虫学习之翻译小程序

Python爬虫学习之翻译小程序

本次博客分享的内容为基于有道在线翻译实现一个实时翻译小程序,本次任务是参考小甲鱼的书《零基础入门学习Python》完成的,书中代码对于当前的有道词典并不适用,使用后无法实现翻译功能,在网...

python3.7简单的爬虫实例详解

python3.7简单的爬虫,具体代码如下所示: #https://www.runoob.com/w3cnote/python-spider-intro.html #Python...

通过python爬虫赚钱的方法

(1)在校大学生。最好是数学或计算机相关专业,编程能力还可以的话,稍微看一下爬虫知识,主要涉及一门语言的爬虫库、html解析、内容存储等,复杂的还需要了解URL排重、模拟登录、验证码识别...