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

所属分类: 脚本专栏 / python 阅读数: 310
收藏 0 赞 0 分享

我有个需求就是抓取一些简单的书籍信息存储到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="{&quot;(.+?)&quot;.*?}"'
  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实现按学生年龄排序的实际问题详解

这篇文章主要给大家介绍了关于Python实现按学生年龄排序实际问题的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧。
收藏 0 赞 0 分享

Python开发的HTTP库requests详解

Requests是用Python语言编写,基于urllib,采用Apache2 Licensed开源协议的HTTP库。它比urllib更加方便,可以节约我们大量的工作,完全满足HTTP测试需求。Requests的哲学是以PEP 20 的习语为中心开发的,所以它比urllib更加P
收藏 0 赞 0 分享

Python网络爬虫与信息提取(实例讲解)

下面小编就为大家带来一篇Python网络爬虫与信息提取(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

在python3环境下的Django中使用MySQL数据库的实例

下面小编就为大家带来一篇在python3环境下的Django中使用MySQL数据库的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python 3.x读写csv文件中数字的方法示例

在我们日常开发中经常需要对csv文件进行读写,下面这篇文章主要给大家介绍了关于Python 3.x读写csv文件中数字的相关资料,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧。
收藏 0 赞 0 分享

Python实现解析Bit Torrent种子文件内容的方法

这篇文章主要介绍了Python实现解析Bit Torrent种子文件内容的方法,结合实例形式分析了Python针对Torrent文件的读取与解析相关操作技巧与注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

Python实现文件内容批量追加的方法示例

这篇文章主要介绍了Python实现文件内容批量追加的方法,结合实例形式分析了Python文件的读写相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Python简单实现自动删除目录下空文件夹的方法

这篇文章主要介绍了Python简单实现自动删除目录下空文件夹的方法,涉及Python针对文件与目录的读取、判断、删除等相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

简单学习Python多进程Multiprocessing

这篇文章主要和大家一起简单的学习Python多进程Multiprocessing ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Python导入模块时遇到的错误分析

这篇文章主要给大家详细解释了在Python处理导入模块的时候出现错误以及具体的情况分析,非常的详尽,有需要的小伙伴可以参考下
收藏 0 赞 0 分享
查看更多