马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
声明:1.可以采用我这里的cookie模拟登陆,用二维码扫描;2.翻页等都可以采用我这模块内容;3.但是我电脑太弱加网速太差(保存cookie我就用了50s,单独在idle运行爬取,直接死机),我最后查找结果是没有出来的,自己试试吧,我不保证from selenium import webdriver
import time
import json
import re
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from pyquery import PyQuery as pq
from config import *
import pymongo
#这里配置了一个MongoDB可以不需要,因为需要把MongoDB配置成服务器,还有config.py,以及MongoDB可视化Robomongo都是要下载的
client = pymongo.MongoClient(MONGO_URL)
db = client[MONGO_DB]
#现在PhantomJS会出现warning,单个使用的话用建议用Chrome,需要配置驱动,具体是将PhantomJS和python.exe放到一起
'''browser = webdriver.PhantomJS(service_args= SERVICE_ARGS)'''
#这个是google的配置,要根据Chrome配置好驱动,和python.exe放到一起
chrome_driver = r'C:\Users\Chysial\AppData\Local\Google\Chrome\Application\chromedriver.exe'
browser = webdriver.Chrome(chrome_driver)
wait = WebDriverWait(browser,10)
browser.set_window_size(1400,900)
browser.get('https://www.taobao.com/')
def get_cookies(browser):
#家里网速不好,时间根据网速自己改,最好用二维码扫描。没有expiry,cookies没有时限,不然就需要修改了
time.sleep(50)
with open('C:\\Users\\Chysial\\Desktop\\cookies.txt','w') as cookief:
cookief.write(json.dumps(browser.get_cookies()))
browser.close()
def open_chrome(browser):
browser.delete_all_cookies()
with open('C:\\Users\\Chysial\\Desktop\\cookies.txt') as cookief:
cookieslist = json.load(cookief)
for cookies in cookieslist:
browser.add_cookie(cookies)
def search():
try:
input = wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR ,'#q'))
)
submit = wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR ,'#J_TSearchForm > div.search-button > button'))
)
input.send_keys("动漫周边")
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 TimeoutException:
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 TimeoutException:
next_page(page_number)
def get_products():
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#mainsrp-item .items .item')))
html = browser.page_source
doc = pq(html)
items = doc('#mainsrp-item .items .item').items()
for item in items:
product = {
'imgage' : item.find('.pic .img').attr('scr'),
'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(rusult):
try:
if db[MONGO_TABLE].insert(result):
print('保存成功')
except Exception :
print('保存MongoDB错误',result)
def main():
# 运行一次就行了,我这里就不重复了get_cookies(browser)
'''get_cookies(browser)'''
open_chrome(browser)
browser.refresh()
total = search()
total = int(re.compile('(\d+)').search(total).group(1))
for i in range(2,total + 1):
next_page(i)
browser.close()
if __name__ == '__main__':
main()
#在项目文件下,配置的config.py文件
'''MONGO_URL = 'localhost'
MONGO_DB = 'taobao'
MONGO_TABLE = "product"
SERVICE_ARGS= ['--load-images = false','--disk-cache = true']'''
|