金狮镖局 Design By www.egabc.com
我有个需求就是抓取一些简单的书籍信息存储到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/" 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">(.+"" else: return items[0] def getProAuthor(html): author=r'<span class="author.{0,20}" data-width="".{0,30}>.*"a-link-normal" href=".*" rel="external nofollow" >(.+"a-color-secondary">(.+"" 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>(.+"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>(.+"" else: return items[0].strip() def getProLanguages(html): languages=r'<li><b>语种:</b>(.+"" 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编程实现使用线性回归预测数据
如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
金狮镖局 Design By www.egabc.com
金狮镖局
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金狮镖局 Design By www.egabc.com
暂无python爬取亚马逊书籍信息代码分享的评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年09月21日
2024年09月21日
- 《年度十大发烧唱片精选 唱片榜获奖作品 2CD》[WAV/分轨][1GB]
- 归龙潮三枚天灯碎片位置在哪 笼律鸟任务天灯图文位置
- 归龙潮新手开荒阵容怎么搭配 新手前期阵容养成攻略
- 归龙潮中浦城区福鼠位置在哪 中浦城区福鼠收集位置大全
- MatteoPasqualini-JohannSebastianBachItalienischesKonzertFranzsischeOuvertureVierDue
- 【雨果唱片】陈百强《梦里人LPCD45》2012[WAV+CUE]
- 王菲.1994-《迷路》美卡引进版[WAV+CUE]
- 老外遇到八戒裸体Bug 网友:我需要用它对付蜘蛛精
- 《黑神话》超强越化补丁:游戏里有文字的地都改了
- 玩家吐槽《黑神话:悟空》唐僧相貌:一脸衰样 没半点气质
- 群星.1994-烧得厉害VOL.2我不是一个人拙滚石】【WAV+CUE】
- 群星.1994-烧得厉害VOL.3风笑痴【滚石】【WAV+CUE】
- 群星.1995-烧得厉害VOL.4两个世界【滚石】【WAV+CUE】
- 归龙潮中浦城区宝箱位置收集大全 中浦区宝箱解密攻略
- 黑神话悟空上品幽灯鬼精魄获取方法一览|上品幽灯鬼精魄收集攻略