Python使用Selenium爬取淘寶異步加載的數(shù)據(jù)方法
淘寶的頁(yè)面很復(fù)雜,如果使用分析ajax或者js的方式,很麻煩
抓取淘寶‘美食'上面的所有食品信息
spider.py
#encoding:utf8
import re
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
from config import *
import pymongo
client=pymongo.MongoClient(MONGODB_URL)
db=client[MONGODB_DB]
##這里使用PhantomJS,并配置了一些參數(shù)
browser=webdriver.PhantomJS(service_args=SERVICE_ArGS)
##窗口的大小,不設(shè)置的話,默認(rèn)太小,會(huì)有問(wèn)題
browser.set_window_size(1400,900)
wait=WebDriverWait(browser, 10)
def search():
print('正在搜索')
##容易出現(xiàn)超時(shí)的錯(cuò)誤
try:
##等待這兩個(gè)模塊都加載好
browser.get("https://www.taobao.com")
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 > div.search-button > button'))
)
######這塊python2搞得鬼
#input.send_keys('\u7f8e\u98df'.decode("unicode-escape"))
input.send_keys(KEYWORD.decode("unicode-escape"))
submit.click()
total = wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR, '#mainsrp-pager > div > div > div > div.total'))
)
get_product()
return total.text
except TimeoutException:
return search()
def next_page(page_number):
print('翻頁(yè)'+str(page_number))
try:
input = wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR, '#mainsrp-pager > div > div > div > div.form > input'))
)
submit=wait.until(
EC.element_to_be_clickable((By.CSS_SELECTOR, '#mainsrp-pager > div > div > div > div.form > span.btn.J_Submit'))
)
input.clear()
input.send_keys(page_number)
submit.click()
##判斷是否翻頁(yè)成功 高亮的是不是輸入的值,直接加在后面即可
wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR,'#mainsrp-pager > div > div > div > ul > li.item.active > span'),str(page_number)))
get_product()
except TimeoutException:
return next_page(page_number)
#獲取產(chǎn)品信息
def get_product():
products = wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR, '#mainsrp-itemlist .m-itemlist .items'))
)
##拿到網(wǎng)頁(yè)
html = browser.page_source
soup = BeautifulSoup(html,'lxml')
items = soup.select('#mainsrp-itemlist .m-itemlist .items .item.J_MouserOnverReq')#
print('*************************到此*************')
for item in items:
img = item.select('.J_ItemPic.img')[0].get('src')
price = item.select('.price.g_price.g_price-highlight > strong')[0].get_text()
deal = item.select('.deal-cnt')[0].get_text()
title= item.select('.row.row-2.title > a ')[0].get_text().strip() #:nth-of-type(3)
shop = item.select('.row.row-3.g-clearfix > .shop > a > span:nth-of-type(2)')[0].get_text()
location = item.select('.location')[0].get_text()
product={
'img':img,
'price':price,
'deal':deal,
'title':title,
'shop':shop,
'location':location
}
#打印一下
import json
j = json.dumps(product)
dict2 = j.decode("unicode-escape")
print dict2
save_to_mongo(product)
def save_to_mongo(product):
try:
if db[MONGODB_TABLE].insert(product):
print('存儲(chǔ)到mongodb成功'+str(product))
except Exception:
print("存儲(chǔ)到mongodb失敗"+str(product))
def main():
try:
total=search()
##搜尋 re正則表達(dá)式
s=re.compile('(\d+)')
total=int(s.search(total).group(1))
for i in range(2,total+1):
next_page(i)
except Exception:
print('出錯(cuò)')
finally:
browser.close()
if __name__ == '__main__':
main()
config.py
MONGODB_URL='localhost' MONGODB_DB='taobao' MONGODB_TABLE='meishi' SERVICE_ArGS=['--load-images=false','--disk-cache=true'] ##就是美食這兩個(gè)字,直接用漢字會(huì)報(bào)錯(cuò) KEYWORD='\u7f8e\u98df'
以上這篇Python使用Selenium爬取淘寶異步加載的數(shù)據(jù)方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python 如何實(shí)時(shí)向文件寫(xiě)入數(shù)據(jù)(附代碼)
這篇文章主要介紹了Python 如何實(shí)時(shí)向文件寫(xiě)入數(shù)據(jù)(附代碼),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
使用python寫(xiě)一個(gè)自動(dòng)瀏覽文章的腳本實(shí)例
今天小編就為大家分享一篇使用python寫(xiě)一個(gè)自動(dòng)瀏覽文章的腳本實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
jupyter notebook 的工作空間設(shè)置操作
這篇文章主要介紹了jupyter notebook 的工作空間設(shè)置操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
基于Python pip用國(guó)內(nèi)鏡像下載的方法
今天小編就為大家分享一篇基于Python pip用國(guó)內(nèi)鏡像下載的方法。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
代碼總結(jié)Python2 和 Python3 字符串的區(qū)別
在本篇文章里小編給大家整理的是一篇關(guān)于Python2 和 Python3 字符串的區(qū)別以及實(shí)例代碼,需要的朋友們學(xué)習(xí)下。2020-01-01
python 使用pdfminer3k 讀取PDF文檔的例子
今天小編就為大家分享一篇python 使用pdfminer3k 讀取PDF文檔的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
python 循環(huán)數(shù)據(jù)賦值實(shí)例
今天小編就為大家分享一篇python 循環(huán)數(shù)據(jù)賦值實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
對(duì)python 合并 累加兩個(gè)dict的實(shí)例詳解
今天小編就為大家分享一篇對(duì)python 合并 累加兩個(gè)dict的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Python編程基礎(chǔ)之類(lèi)和對(duì)象
這篇文章主要為大家詳細(xì)介紹了Python的類(lèi)和對(duì)象,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-01-01

