selenium爬取淘宝商品信息存储到MongoDB
程序员文章站
2022-05-12 20:04:44
...
1、流程框架
搜索关键字
分析页码并翻页
分析提取商品内容
存储至MongoDB
2、spider.py
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
import re
from pyquery import PyQuery as pq
from taobaomeishi.config import *
import pymongo
client=pymongo.MongoClient(MONGO_URL)
db=client[MONGO_DB]
browser=webdriver.Chrome()
# browser=webdriver.PhantomJS() #PhantomJS
wait=WebDriverWait(browser,10)
def search():
try:
browser.get('https://www.taobao.com')
input= wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR, "#q"))
)
submit=WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#J_TSearchForm > div.search-button > button')))
input.send_keys(KEY_WORD)
submit.click()
total=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#mainsrp-pager > div > div > div > div.total')))
get_products()
return total.text
except TimeoutError:
return search()
def next_page(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.presence_of_element_located((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_products()
except TimeoutError:
next_page(page_number)
def get_products():
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#mainsrp-itemlist .items .item')))
html=browser.page_source
doc=pq(html)
items=doc('#mainsrp-itemlist .items .item').items()
for item in items:
product={
'image':item.find('.pic .img').attr('src'),
'price':item.find('.price').text(),
'deal':item.find('.deal-cnt').text()[:-3],
'title':item.find('.title').text(),
'shop':item.find('.shop').text(),
'location':item.find('.location').text()
}
print(product)
save_to_mongo(product)
def save_to_mongo(result):
try:
if db[MONGO_TABLE].insert(result):
print('存储到mongo成功',result)
except Exception:
print("存储到mongo失败",result)
def main():
try:
total=search()
total=int(re.compile('(\d+)').search(total).group(1))
for i in range(2,total+1):
next_page(i)
except Exception:
print("出错了")
finally:
browser.close()
if __name__== "__main__":
main()
3、config、py
MONGO_URL='localhost'
MONGO_DB='taobao'
MONGO_TABLE='product'
KEY_WORD='美食'
4、存储到MongoDB
推荐阅读
-
Selenium爬取淘宝商品概要入mongodb
-
【爬虫学习三】 Python将爬取的数据存储到MongoDB中
-
爬取实习僧网站并存储到MongoDB
-
【Python Scrapy 爬虫框架】 5、利用 pipelines 和 settings 将爬取数据存储到 MongoDB
-
爬取贝壳租房信息存储到mongoDB
-
Scrapy:在Scrapy中使用selenium来爬取简书全站内容,并存储到MySQL数据库中
-
Selenium爬取淘宝商品概要入mongodb
-
selenium爬取淘宝商品信息存储到MongoDB
-
使用python利器selenium工具模拟浏览器运行并爬取淘宝商品信息
-
使用selenium爬取拉勾网数据分析职位信息并存储到MongoDB