python爬取亚马逊书籍信息代码分享
作者:天下醉闲 发布时间:2021-03-25 09:38:14
标签: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();
总结
matplotlib在python上绘制3D散点图实例详解
python的unittest测试类代码实例
Python编程实现使用线性回归预测数据
如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
来源:http://blog.csdn.net/tianxiazuixian/article/details/53322240


猜你喜欢
- 微软建议用Request.BinaryRead()读取表单数据,但由于这种方法读出的是二进制数据,需要对读出的数据逐字节进行分析,生成有意义
- 前言在线演示地址:http://haiyong.site/age-calculatorJavaScript提供了一些内置的日期和时间函数,有
- 摘要:本篇博客介绍了YOLOv5车牌识别的理论基础,包括目标检测的概念、YOLO系列的发展历程、YOLOv5的网络结构和损失函数等。通过深入
- 看了上一篇内容之后,相信对K近邻算法有了一个清晰的认识,今天的内容——手写数字识别是对上一篇内容的延续,这里也是为了自己能更熟练的掌握k-N
- 升级目前php最新版虽然是php5.5,但出于各种考虑,还是先升到php5.4比较靠谱。原php使用的是php5.2.10,已经运行了4~5
- JavaScript中的64位加密及解密的两个方法。function base64Encode(text){if (/(
- 本文实例讲述了python飞机大战pygame碰撞检测实现方法。分享给大家供大家参考,具体如下:目标了解碰撞检测方法碰撞实现01. 了解碰撞
- 这个是用python实现的基本的增删改查的学生管理系统,其中主要是对输入的数据进行合法性检测的问题,这次又对函数进行了练习!掌握函数更加熟练
- 什么是Nacos英文全称Dynamic Naming and Configuration Service,Na为naming/nameSer
- Python代码集体右移的方法:直接选中需要右移的代码,按tab键即可。Python中代码集体左移的方法:直接选中需要左移的代码,按shif
- 导语哈喽!我是木木子,今天又想我了嘛?之前不是出过一期Python美颜相机嘛?不知道你们还记得不?这一期的话话题还是围绕上期关于颜值方面来走
- 前言由与上不了学,教我们Mastercam的老师提前给我们布置了4道习题。对我们太好了,谢谢老师�没办法,干就完了。只是要求附上制作过程视频
- 区别IE6与FF:background:orange;*background:blue;区别IE6与IE7:background:green
- 一、概念它们都是Element的属性,表示元素的宽度:Element.clientWidth 内容+内边距-滚动条
- CSSer与其他IT职位一样,在找工作的时候,都会面临着面试官提出的问题,或者给出的试卷。一、超链接点击过后hover样式就不出现的问题?被
- js数组元素的添加和删除一直比较迷惑,今天终于找到详细说明的资料了,先给个我测试的代码^-^var arr = new Array();ar
- remove 删除单个元素,删除首个符合条件的元素,按值删除,返回值为空List_remove = [1, 2, 2, 2, 3, 4]pr
- Gogland 是 JetBrains 公司推出的Go语言集成开发环境。Gogland 同样基于 IntelliJ 平台开发,支持 JetB
- 上文:栅格:从混乱到秩序Jacci Howard Bear 的英文原文:http://desktoppub.about.com/od/gri
- 参照网上资料在CentOS6.8服务器上使用cmake安装了MySQL5.7.18,安装过程中遇到了各种各样的问题,大多问题在网上都能找到解