python爬取淘宝商品销量信息
作者:平步青云win 时间:2023-06-01 14:59:03
python爬取淘宝商品销量的程序,运行程序,输入想要爬取的商品关键词,在代码中的‘###'可以进一步约束商品的属性,比如某某作者的书籍,可以在###处输入作者名字,以及时期等等。最后可以得到所要商品的总销量
import requests
import bs4
import re
import json
def open(keywords, page):
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"}
payload = {'q':keywords, 'sort':"sale-desc", 's':(page-1)*44}
url = "https://s.taobao.com/search"
res = requests.get(url, params = payload)
return res
def get_item(res):
g_page_config = re.search(r'g_page_config = (.*?);\n', res.text)
page_config_json = json.loads(g_page_config.group(1))
page_item = page_config_json['mods']['itemlist']['data']['auctions']
result = []#整理出我们关注的信息(ID,标题,链接,售价,销量和商家)
for each in page_item:
dict1 = dict.fromkeys(('id','title','link','price','sale','shoper'))
dict1['id'] = each['nid']
dict1['title'] = each['title']
dict1['link'] = each['detail_url']
dict1['price'] = each['view_price']
dict1['sale'] = each['view_sales']
dict1['shoper'] = each['nick']
result.append(dict1)
return result
def count_sales(items):
count = 0
for each in items:
if '###' in each['title']:#规定只取标题中‘###'的商品
count += int(re.search(r'\d+',each['sale']).group())
return count
def main():
keywords = input("请输入搜索关键词:")#可以为各种商品名称
length = 10#淘宝商品页数
total = 0
for each in range(length):
res = open(keywords, each+1)
items = get_item(res)
total += count_sales(items)#销售总量
print(total)
if __name__ == "__main__":
main()
来源:https://blog.csdn.net/qq_25774883/article/details/81292383
标签:python,爬取,淘宝
0
投稿
猜你喜欢
基于Python编写简易版的天天跑酷游戏的示例代码
2023-10-23 01:10:11
python等间距取值方式
2022-03-15 23:19:26
python实现发送邮件功能代码
2023-08-25 16:05:10
网页布局设计基础
2008-10-16 13:58:00
详解Python爬取并下载《电影天堂》3千多部电影
2023-07-07 23:48:24
Python机器学习应用之决策树分类实例详解
2021-12-19 16:57:53
pandas 把数据写入txt文件每行固定写入一定数量的值方法
2021-06-13 20:08:14
Informatica bulk与normal模式的深入详解
2024-01-16 01:30:28
通过分析SQL语句的执行计划优化SQL
2011-10-24 20:03:20
MySQL中的随机抽取的实现
2024-01-17 21:07:42
tensorflow 自定义损失函数示例代码
2023-03-13 21:37:18
Python设计实现的计算器功能完整实例
2021-07-17 12:26:05
JS实现仿百度文库评分功能
2024-05-11 09:34:05
package.json版本号符号^和~前缀的区别
2024-05-02 17:36:11
关于php开启错误提示的总结
2023-11-04 10:46:21
2011年网页设计发展趋势
2011-01-10 20:45:00
详解pandas库pd.read_excel操作读取excel文件参数整理与实例
2021-08-07 00:06:22
Python三十行代码实现简单人脸识别的示例代码
2022-08-29 18:07:49
mysqldumpslow用法示例(慢查询)
2024-01-15 22:42:03
mysql学习笔记之基础知识
2024-01-13 08:27:16