淘宝的页面很复杂,如果使用分析ajax或者js的方式,很麻烦
抓取淘宝‘美食'上面的所有食品信息
spider.py
#encoding:utf8import refrom selenium import webdriverfrom selenium.common.exceptions import TimeoutExceptionfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom bs4 import BeautifulSoupfrom config import *import pymongoclient=pymongo.MongoClient(MONGODB_URL)db=client[MONGODB_DB]##这里使用PhantomJS,并配置了一些参数browser=webdriver.PhantomJS(service_args=SERVICE_ArGS)##窗口的大小,不设置的话,默认太小,会有问题browser.set_window_size(1400,900)wait=WebDriverWait(browser, 10)def search(): print('正在搜索')##容易出现超时的错误 try:##等待这两个模块都加载好 browser.get("https://pile('(\d+)') total=int(s.search(total).group(1)) for i in range(2,total+1): next_page(i) except Exception: print('出错') 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']##就是美食这两个字,直接用汉字会报错KEYWORD='\u7f8e\u98df'以上这篇Python使用Selenium爬取淘宝异步加载的数据方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。