python爬取淘宝商品销量信息

yipeiwu_com6年前Python爬虫

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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python爬虫_实现校园网自动重连脚本的教程

python爬虫_实现校园网自动重连脚本的教程

一、背景 最近学校校园网不知道是什么情况,总出现掉线的情况。每次掉线都需要我手动打开web浏览器重新进行账号密码输入,重新进行登录。系统的问题我没办法解决,但是可以写一个简单的pytho...

python爬虫基本知识

爬虫简介       根据百度百科定义:网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照...

Python爬虫实现获取动态gif格式搞笑图片的方法示例

本文实例讲述了Python爬虫实现获取动态gif格式搞笑图片的方法。分享给大家供大家参考,具体如下: 有时候看到一些喜欢的动图,如果一个个取保存挺麻烦,有的网站还不支持右键保存,因此使用...

Python数据抓取爬虫代理防封IP方法

Python数据抓取爬虫代理防封IP方法

爬虫:一段自动抓取互联网信息的程序,从互联网上抓取对于我们有价值的信息,一般来说,Python爬虫程序很多时候都要使用(飞猪IP)代理的IP地址来爬取程序,但是默认的urlopen是无法...

Python爬取网易云音乐热门评论

Python爬取网易云音乐热门评论

最近在研究文本挖掘相关的内容,所谓巧妇难为无米之炊,要想进行文本分析,首先得到有文本吧。获取文本的方式有很多,比如从网上下载现成的文本文档,或者通过第三方提供的API进行获取数据。但是有...