這期內容當中小編將會給大家帶來有關使用Python怎么爬取淘寶的商品信息,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
python常用的庫:1.requesuts;2.scrapy;3.pillow;4.twisted;5.numpy;6.matplotlib;7.pygama;8.ipyhton等。
1、導入需要的包
import timefrom bs4 import BeautifulSoupfrom selenium import webdriver
2、編輯淘寶數據抓取的函數邏輯
# 編輯淘寶數據抓取的函數邏輯 """ 1.登錄淘寶 2.首頁 3.指定商品的搜索信息 4.提取指定商品的金額、購買人數、商鋪地址、商品名稱、商品圖片 """
3、打開淘寶網頁,并掃碼登陸點擊淘寶網首頁
def login_info(self): # 1.打開網頁 self.browser.get(self.url) # 2.通過掃碼的形式去登錄淘寶賬號 if self.browser.find_element_by_xpath('//*[@id="login"]/div[1]/i'): self.browser.find_element_by_xpath('//*[@id="login"]/div[1]/i').click() # 讓程序等待休眠5秒,通過手機掃碼登錄 time.sleep(8) # 3.點擊淘寶網首頁 taobao_index = self.browser.find_element_by_xpath('//*[@id="J_SiteNavHome"]/div/a') taobao_index.click() time.sleep(1)
4、自動的在淘寶首頁中輸入自己想要搜索的商品名稱,并且自動點擊搜索
search_input = self.browser.find_element_by_xpath('//*[@id="q"]') shop_name = input("請輸入你想搜索的商品名稱:") search_input.send_keys(shop_name) time.sleep(0.5) search_submit = self.browser.find_element_by_xpath('//*[@id="J_TSearchForm"]/div[1]/button') search_submit.click()
5、獲取商品信息
# 商品金額 shop_price_data = shop_data.find_all('div', class_='price g_price g_price-highlight') for shop_price in shop_price_data: shop_price_list.append(shop_price.text.strip()) # 購買人數 shop_people_number_data = shop_data.find_all('div','deal-cnt') for shop_people_number in shop_people_number_data: shop_people_list.append(shop_people_number.text) # 店鋪地區 shop_location_data = shop_data.find_all('div','location') for shop_location in shop_location_data: shop_location_list.append(shop_location.text)
上述就是小編為大家分享的使用Python怎么爬取淘寶的商品信息了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。