資源簡介
利用python爬蟲爬取淘寶粽子銷售數據,并對其分析,得到粽子商品名稱詞云圖、粽子店鋪銷量Top10、粽子商品銷量Top10、各省份粽子銷量分布

代碼片段和文件信息
#?-*-?coding:?utf-8?-*-
“““
Created?on?Wed?Jun?17?22:29:52?2020
@author:?朱小五
微信公眾號:?凹凸數據
公眾號ID:?alltodata
“““
from?selenium?import?webdriver
import?time
import?csv
import?re
#?搜索商品,獲取商品頁碼
def?search_product(key_word):
????#?定位輸入框
????browser.find_element_by_id(“q“).send_keys(key_word)
????#?定義點擊按鈕,并點擊
????browser.find_element_by_class_name(‘btn-search‘).click()
????#?最大化窗口:為了方便我們掃碼
????browser.maximize_window()
????#?等待15秒,給足時間我們掃碼
????time.sleep(15)
????#?定位這個“頁碼”,獲取“共100頁這個文本”
????page_info?=?browser.find_element_by_xpath(‘//*[@id=“mainsrp-pager“]/div/div/div/div[1]‘).text
????#?需要注意的是:findall()返回的是一個列表,雖然此時只有一個元素它也是一個列表。
????page?=?re.findall(“(\d+)“?page_info)[0]
????return?page
#?獲取數據
def?get_data():
????#?通過頁面分析發現:所有的信息都在items節點下
????items?=?browser.find_elements_by_xpath(‘//div[@class=“items“]/div[@class=“item?J_MouserOnverReq??“]‘)
????for?item?in?items:
????????#?參數信息
????????pro_desc?=?item.find_element_by_xpath(‘.//div[@class=“row?row-2?title“]/a‘).text
????????#?價格
????????pro_price?=?item.find_element_by_xpath(‘.//strong‘).text
????????#?付款人數
????????buy_num?=?item.find_element_by_xpath(‘.//div[@class=“deal-cnt“]‘).text
????????#?旗艦店
????????shop?=?item.find_element_by_xpath(‘.//div[@class=“shop“]/a‘).text
????????#?發貨地
????????address?=?item.find_element_by_xpath(‘.//div[@class=“location“]‘).text
????????#?print(pro_desc?pro_price?buy_num?shop?address)
????????with?open(‘{}.csv‘.format(key_word)?mode=‘a‘?newline=‘‘?encoding=‘utf-8-sig‘)?as?f:
????????????csv_writer?=?csv.writer(f?delimiter=‘‘)
????????????csv_writer.writerow([pro_desc?pro_price?buy_num?shop?address])
def?main():
????browser.get(‘https://www.taobao.com/‘)
????page?=?search_product(key_word)
????print(page)
????get_data()
????page_num?=?1
????while?int(page)?!=?page_num:
????????print(“*“?*?100)
????????print(“正在爬取第{}頁“.format(page_num?+?1))
????????browser.get(‘https://s.taobao.com/search?q={}&s={}‘.format(key_word?page_num?*?44))
????????browser.implicitly_wait(15)
????????get_data()
????????page_num?+=?1
????print(“數據爬取完畢!“)
if?__name__?==?‘__main__‘:
????key_word?=?“粽子“
????browser?=?webdriver.Chrome(executable_path=“C:\Program?Files?(x86)\Google\Chrome\Application\chromedriver.exe“)
????main()
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2020-07-02?00:51??粽子\
?????文件????15067744??2007-09-17?14:50??粽子\msyh.ttf
?????文件???????45124??2020-06-18?17:17??粽子\stop_words.txt
?????文件????????6281??2020-07-01?00:03??粽子\不同價格區間的粽子銷量占比.html
?????文件????????9571??2020-07-01?00:03??粽子\各省份粽子銷量分布.html
?????文件????????2666??2020-06-30?18:04??粽子\淘寶爬蟲-粽子.py
?????文件??????529131??2020-06-30?18:57??粽子\清洗完成數據.csv
?????文件????????1513??2020-06-30?18:57??粽子\粽子-數據清洗.py
?????文件??????599806??2020-06-30?18:33??粽子\粽子.csv
?????文件???????35064??2020-07-01?00:03??粽子\粽子商品名稱詞云圖.html
?????文件????????7226??2020-07-01?00:03??粽子\粽子商品銷量Top10.html
?????文件????????5879??2020-07-01?00:03??粽子\粽子店鋪銷量Top10.html
?????文件????????5339??2020-07-02?00:51??粽子\粽子數據分析.py
評論
共有 條評論