科技改變生活 · 科技引領(lǐng)未來
現(xiàn)如今,我們已經(jīng)離不開網(wǎng)上購(gòu)物,足不出戶就可享受線上購(gòu)物,大大方便了我們的生活。電商app都是大家手機(jī)上的必備軟件,像作為消費(fèi)主力的年輕人,幾乎每天都要打開淘寶瀏覽商品信息。其實(shí),使用python爬蟲就可以實(shí)現(xiàn)爬取淘寶的商品信息,本文將分步驟向大家介紹爬取淘寶的商品信息的過程。
1、導(dǎo)入需要的包
import timefrom bs4
import BeautifulSoupfrom selenium
import webdriver
2、編輯淘寶數(shù)據(jù)抓取的函數(shù)邏輯
編輯淘寶數(shù)據(jù)抓取的函數(shù)邏輯
&34;&34;&34;
1.登錄淘寶
2.首頁(yè)
3.指定商品的搜索信息
4.提取指定商品的金額、購(gòu)買人數(shù)、商鋪地址、商品名稱、商品圖片
&34;&34;&34;
3、打開淘寶網(wǎng)頁(yè),并掃碼登陸點(diǎn)擊淘寶網(wǎng)首頁(yè)
def login_info(self):
1.打開網(wǎng)頁(yè)
self.browser.get(self.url)
2.通過掃碼的形式去登錄淘寶賬號(hào)
if self.browser.find_element_by_xpath(&39;//*[@id=&34;login&34;]/div[1]/i&39;):
self.browser.find_element_by_xpath(&39;//*[@id=&34;login&34;]/div[1]/i&39;).click()
讓程序等待休眠5秒,通過手機(jī)掃碼登錄
time.sleep(8)
3.點(diǎn)擊淘寶網(wǎng)首頁(yè)
taobao_index = self.browser.find_element_by_xpath(&39;//*[@id=&34;J_SiteNavHome&34;]/div/a&39;)
taobao_index.click()
time.sleep(1)
4、自動(dòng)地在淘寶首頁(yè)中輸入自己想要搜索的商品名稱,并且自動(dòng)點(diǎn)擊搜索
search_input = self.browser.find_element_by_xpath(&39;//*[@id=&34;q&34;]&39;)
shop_name = input(&34;請(qǐng)輸入你想搜索的商品名稱:&34;)
search_input.send_keys(shop_name)
time.sleep(0.5)
search_submit = self.browser.find_element_by_xpath(&39;//*[@id=&34;J_TSearchForm&34;]/div[1]/button&39;)
search_submit.click()
5、獲取商品信息
商品金額
shop_price_data = shop_data.find_all(&39;div&39;, class_=&39;price g_price g_price-highlight&39;)
for shop_price in shop_price_data:
shop_price_list.append(shop_price.text.strip())
購(gòu)買人數(shù)
shop_people_number_data = shop_data.find_all(&39;div&39;,&39;deal-cnt&39;)
for shop_people_number in shop_people_number_data:
shop_people_list.append(shop_people_number.text)
店鋪地區(qū)
shop_location_data = shop_data.find_all(&39;div&39;,&39;location&39;)
for shop_location in shop_location_data:
shop_location_list.append(shop_location.text)
代理IP質(zhì)量穩(wěn)定,我們才能夠更好更快地去執(zhí)行工作任務(wù),例如神龍,多平臺(tái)適用,遍布全國(guó)的自營(yíng)服務(wù)器節(jié)點(diǎn),可以更好的輔助爬蟲。
了解更多馬夕一
版權(quán)所有 未經(jīng)許可不得轉(zhuǎn)載
增值電信業(yè)務(wù)經(jīng)營(yíng)許可證備案號(hào):遼ICP備14006349號(hào)
網(wǎng)站介紹 商務(wù)合作 免責(zé)聲明 - html - txt - xml