Python使用Selenium爬取淘宝异步加载的数据方法
程序员文章站
2023-10-28 09:25:34
淘宝的页面很复杂,如果使用分析ajax或者js的方式,很麻烦
抓取淘宝‘美食'上面的所有食品信息
spider.py
#encoding:utf8
impo...
淘宝的页面很复杂,如果使用分析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,并配置了一些参数 browser=webdriver.phantomjs(service_args=service_args) ##窗口的大小,不设置的话,默认太小,会有问题 browser.set_window_size(1400,900) wait=webdriverwait(browser, 10) def search(): print('正在搜索') ##容易出现超时的错误 try: ##等待这两个模块都加载好 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('翻页'+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() ##判断是否翻页成功 高亮的是不是输入的值,直接加在后面即可 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) #获取产品信息 def get_product(): products = wait.until( ec.presence_of_element_located((by.css_selector, '#mainsrp-itemlist .m-itemlist .items')) ) ##拿到网页 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('存储到mongodb成功'+str(product)) except exception: print("存储到mongodb失败"+str(product)) def main(): try: total=search() ##搜寻 re正则表达式 s=re.compile('(\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爬取淘宝异步加载的数据方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。