這篇文章主要介紹python3怎么實(shí)現(xiàn)爬取淘寶美食,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
python的五大特點(diǎn):1.簡單易學(xué),開發(fā)程序時,專注的是解決問題,而不是搞明白語言本身。2.面向?qū)ο?,與其他主要的語言如C++和Java相比, Python以一種非常強(qiáng)大又簡單的方式實(shí)現(xiàn)面向?qū)ο缶幊獭?.可移植性,Python程序無需修改就可以在各種平臺上運(yùn)行。4.解釋性,Python語言寫的程序不需要編譯成二進(jìn)制代碼,可以直接從源代碼運(yùn)行程序。5.開源,Python是 FLOSS(自由/開放源碼軟件)之一。
環(huán)境:
ubuntu16.04
python3.5
python庫: selenium, pyquery,pymongo, re
要求:
設(shè)置×××面瀏覽器訪問,并將商品列表存入mongoDB數(shù)據(jù)庫.
分析過程暫時略過
代碼:
#!/usr/bin/env python # -*- coding:utf-8 -*- """ 1.爬取淘寶美食的流程 - 搜索關(guān)鍵字: 用selenium打開瀏覽器,模擬輸入關(guān)鍵字,并搜索對應(yīng)的商品列表. - 分析頁碼并翻頁,模擬翻頁,查看到所有頁面的商品列表. - 分析并提取商品,利用Pyquery分析源碼,解析得到商品列表. - 存儲到MONGODB數(shù)據(jù)庫,將商品列表信息存儲到mongoDB數(shù)據(jù)庫. """ from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException from pyquery import PyQuery as pq import re from anli.mongoconfig import * import pymongo client = pymongo.MongoClient(MONGO_URL) db = client[MONGO_DB] #設(shè)置×××面訪問 opt = webdriver.FirefoxOptions() opt.set_headless() browser = webdriver.Firefox(options=opt) #等待瀏覽器加載頁面成功. wait = WebDriverWait(browser,10) def search(): try: # 后臺打開瀏覽器 browser.get('https://www.taobao.com') # 用CSS選擇器復(fù)制搜索框 input = wait.until( EC.presence_of_element_located((By.CSS_SELECTOR, '#q')) ) # 找到搜索按鈕. submit = wait.until( # EC.element_to_be_clickable((By.CSS_SELECTOR,'#J_TSearchForm .search-button'))) EC.element_to_be_clickable((By.CSS_SELECTOR,'#J_TSearchForm > div.search-button > button'))) # 輸入關(guān)鍵字 input.send_keys('美食') # 點(diǎn)擊搜索按鈕. submit.click() # 輸出總共的頁數(shù). total=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'.total'))) # 調(diào)取商品列表的函數(shù). get_products() return total.text except TimeoutException: #超時錯誤. return search() # 翻頁 def next_page(page_number): try: #注意在firefox和chrome瀏覽器復(fù)制出來的元素不太一樣. #要傳入的頁碼: 到第幾頁 input = wait.until( EC.presence_of_element_located((By.CSS_SELECTOR,'input.input:nth-child(2)')) ) #復(fù)制確定按鈕的元素: submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'span.btn:nth-child(4)'))) #清除頁碼 input.clear() #輸入當(dāng)前頁碼 input.send_keys(page_number) #點(diǎn)擊確定按鈕. submit.click() #判斷當(dāng)前頁碼是否是當(dāng)前數(shù)字: 復(fù)制高亮頁碼數(shù). wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR,'span.num'),str(page_number))) get_products() except TimeoutException: next_page(page_number) #解析jquery源碼 def get_products(): #商品列表: wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#mainsrp-itemlist .items .item'))) # 獲取網(wǎng)頁源碼 html = browser.page_source doc = pq(html) items = doc('#mainsrp-itemlist .items .item').items() for item in items: #定義商品列表詳細(xì)信息的字典 product = { 'title': item.find('.title').text(), 'price': item.find('.price').text(), 'image': item.find('.pic .img').attr('src'), 'shop': item.find('.shop').text(), 'deal': item.find('.deal-cnt').text()[:-3], 'location': item.find('.location').text() } print(product) #將商品列表信息保存到mongoDB數(shù)據(jù)庫. save_to_mongo(product) def save_to_mongo(result): try: if db[MONGO_TABLE].insert(result): print('存儲到mongodb成功',result) except Exception: print('存儲到mongodb失敗',result) def main(): total = search() # 用正則表達(dá)式只匹配出數(shù)字,并打印數(shù)字. total = int(re.compile('(\d+)').search(total).group(1)) print(total) for i in range(2,total + 1): next_page(i) if __name__=='__main__': main() #關(guān)閉×××面瀏覽器. browser.quit() #mongoconfig.py #!/usr/bin/env python # -*- coding:utf-8 -*- MONGO_URL = 'localhost' MONGO_DB = 'taobao' MONGO_TABLE = 'meishi'
實(shí)現(xiàn)效果:
注意:
chrome瀏覽器和firefox瀏覽器的網(wǎng)頁元素不太一樣.
以上是“python3怎么實(shí)現(xiàn)爬取淘寶美食”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享文章:python3怎么實(shí)現(xiàn)爬取淘寶美食-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://jinyejixie.com/article32/gposc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、App設(shè)計、網(wǎng)站收錄、品牌網(wǎng)站制作、標(biāo)簽優(yōu)化、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容