Python通过正则库爬取淘宝商品信息代码实例
作者:江武555 时间:2021-12-09 18:49:55
使用正则库爬取淘宝商品的商品信息,首先我们需要确定想要爬取的对象
我们在淘宝里搜索“python”,出来的结果
从url连接中可以得到搜索商品的关键字是“q=”,所以我们要用的起始url为:https://s.taobao.com/search?q=python
然后翻页,经过对比发现,翻页后,变化的关键字是s,每次翻页,s便以44的倍数增长(可以数一下每页显示的商品数量,刚好是44)
所以可以根据关键字“s=”,来设置爬取的深度(爬取多少页)
右键查看源码,商品名称可能的关键字是“title”和“raw_title”,进一步多看几个商品的名称,发现选取“raw_title”比较合适;商品价格自然就是“view_price”(通过比对淘宝商品展示页面);所以商品名称和商品价格分别是以"raw_title":"名称"和"view_price":"价格",这样的键/值对的形式展示的。
# coding:utf-8
import requests
import re
goods = '水杯'
url = 'https://s.taobao.com/search?q=' + goods
r = requests.get(url=url, timeout=10)
html = r.text
tlist = re.findall(r'\"raw_title\"\:\".*?\"', html) # 正则提取商品名称
plist = re.findall(r'\"view_price\"\:\"[\d\.]*\"', html) # 正则提示商品价格
print(tlist)
print(plist)
print(type(plist)) # 正则表达式提取出的商品名称和商品价格都是以列表形式存储数据的
利用for循环,把每个商品的名称和价格组成一个列表,然后把这写列表再追加到一个大列表中:
goodlist = []
for i in range(len(tlist)):
title = eval(tlist[i].split(':')[1]) # eval()函数简单说就是用于去掉字符串的引号
price = eval(plist[i].split(':')[1])
goodlist.append([title, price]) # 把每个商品的名称和价格组成一个小列表,然后把所有商品组成的列表追加到一个大列表中
print(goodlist)
大概的思路就是这样的。
def get_html(url):
"""获取源码html"""
try:
r = requests.get(url=url, timeout=10)
r.encoding = r.apparent_encoding
return r.text
except:
print("获取失败")
def get_data(html, goodlist):
"""使用re库解析商品名称和价格
tlist:商品名称列表
plist:商品价格列表"""
tlist = re.findall(r'\"raw_title\"\:\".*?\"', html)
plist = re.findall(r'\"view_price\"\:\"[\d\.]*\"', html)
for i in range(len(tlist)):
title = eval(tlist[i].split(':')[1]) # eval()函数简单说就是用于去掉字符串的引号
price = eval(plist[i].split(':')[1])
goodlist.append([title, price])
def write_data(list, num):
# with open('E:/Crawler/case/taob2.txt', 'a') as data:
# print(list, file=data)
for i in range(num): # num控制把爬取到的商品写进多少到文本中
u = list[i]
with open('E:/Crawler/case/taob.txt', 'a') as data:
print(u, file=data)
def main():
goods = '水杯'
depth = 3 # 定义爬取深度,即翻页处理
start_url = 'https://s.taobao.com/search?q=' + goods
infoList = []
for i in range(depth):
try:
url = start_url + '&s=' + str(44 * i) # 因为淘宝显示每页44个商品,第一页i=0,一次递增
html = get_html(url)
get_data(html, infoList)
except:
continue
write_data(infoList, len(infoList))
if __name__ == '__main__':
main()
来源:https://www.cnblogs.com/jackyfive/p/12046136.html
标签:Python,正则,爬取,淘宝
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
各个版本IE合集下载,共存无冲突
2007-11-29 13:12:00
![](https://img.aspxhome.com/file/UploadPic/200711/29/20071129132016783s.png)
asp如何读取文本文件的内容?
2009-11-18 20:55:00
用户体验杂谈
2011-10-21 21:09:08
js保留两位小数方法总结
2023-07-18 17:34:35
PHP设计模式之观察者模式浅析
2023-05-27 12:20:11
Microsoft SQL Server 安全问题
2010-07-26 13:20:00
如何使用PHP计算上一个月的今天
2023-11-15 14:16:25
php全局变量和类配合使用深刻理解
2023-11-18 19:50:17
python 搭建简单的http server,可直接post文件的实例
2021-08-25 15:07:39
Python 私有属性和私有方法应用场景分析
2023-12-06 05:36:42
Safari参考样式库之webkit
2009-07-26 09:50:00
![](https://img.aspxhome.com/file/UploadPic/20097/26/webkit1-13s.jpg)
交互设计实用指南系列(9)—一次点击
2010-02-08 12:42:00
![](https://img.aspxhome.com/file/UploadPic/20102/8/guanji-80s.jpg)
使用SQL Server 2008中对象相关性
2008-12-12 06:36:00
Python实战之手写一个搜索引擎
2023-07-11 21:16:49
![](https://img.aspxhome.com/file/2023/3/67323_0s.jpg)
python输入错误后删除的方法
2023-07-25 11:55:09
如何实现My SQL中的用户的管理问题
2008-12-03 13:56:00
python自动提取文本中的时间(包含中文日期)
2023-08-22 21:32:11
JDBC连接MySQL数据库关键的四个步骤
2009-12-17 12:06:00
JavaScript开发时的五个小提示
2007-11-21 19:54:00
交互设计模式(二)-Pagination(分页,标记页数)
2009-08-03 13:37:00
![](https://img.aspxhome.com/file/UploadPic/20098/3/30_0_0_427-69s.jpg)