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

作者:天下醉闲 时间:2021-03-25 09:38:14 

我有个需求就是抓取一些简单的书籍信息存储到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();

总结

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

python的unittest测试类代码实例

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

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

来源:http://blog.csdn.net/tianxiazuixian/article/details/53322240

标签:python,爬虫,爬取网页数据
0
投稿

猜你喜欢

  • SQL Server 2005改进后的几个实用新特性

    2008-05-07 19:16:00
  • 详细分析mysql MDL元数据锁

    2024-01-28 18:29:10
  • Oracle数据库逻辑备份的SH文件

    2010-07-27 13:26:00
  • 三种在ES6中将非数组转换为数组的方法详情

    2024-04-16 08:55:06
  • 浅谈Python编程中3个常用的数据结构和算法

    2022-02-11 20:15:04
  • python 用matplotlib绘制折线图详情

    2022-03-23 16:38:19
  • JavaScript字典与集合详解

    2024-04-16 09:28:13
  • python:print格式化输出到文件的实例

    2023-08-25 06:40:53
  • 深入透析样式表滤镜(上)

    2011-06-14 09:48:40
  • Python科学计算之Pandas详解

    2023-07-21 19:12:51
  • Python如何省略括号方法详解

    2022-12-11 07:23:26
  • 使用Python实现下载网易云音乐的高清MV

    2022-08-07 17:30:52
  • Python计算两个日期相差天数的方法示例

    2023-05-17 13:59:21
  • pycharm显示远程图片的实现

    2021-03-02 13:27:39
  • 如何安装多版本python python2和python3共存以及pip共存

    2021-03-23 05:08:58
  • oracle数据排序后获取前几行数据的写法(rownum、fetch方式)

    2023-07-02 01:15:09
  • 聊聊Javascript中try catch的2个作用

    2024-04-22 13:25:57
  • Python 正则表达式爬虫使用案例解析

    2022-08-08 20:30:39
  • 教你Pycharm安装使用requests第三方库的详细教程

    2023-02-24 16:40:01
  • 详细介绍Scrapy shell的使用教程

    2022-04-18 03:35:39
  • asp之家 网络编程 m.aspxhome.com