您好,登錄后才能下訂單哦!
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()
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。