import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from tqdm import tqdm
Options = webdriver.ChromeOptions()
Options.add_experimental_option('detach', True)
borser = webdriver.Chrome(service=Service(executable_path='./chromedriver.exe'), options=Options)
URL = 'http://www.taobao.com'
borser.get(url=URL)
time.sleep(3)
with open(r'./cookies.txt', 'r', encoding='utf-8') as file:
cookie_file = file.read()
cookies = eval(cookie_file)
# print(cookies)
"""
cookie具有时效性,失效了就重新爬取
"""
for i in cookies:
borser.add_cookie(i)
borser.refresh()
goods = input("请输入商品名:")
borser.find_element(By.CSS_SELECTOR, '#q').send_keys(goods)
# 这个click可以成功,是因为没有遮挡物
borser.find_element(
By.CSS_SELECTOR, '#J_TSearchForm > div.search-button > button').click()
# 瀑布式加载:网速不好时,页面显示商品不全,爬虫获取到的信息就会有缺失
# 未显示在窗口的先不加载,实现页面的滚动,
y = 0
while True:
y += 500
borser.execute_script(f'window.scrollTo(0, {y})')
time.sleep(1) # 防止被检测到是爬虫
if y > 6000:
break
"""
若构造不出翻页链接,可以使用selenium点击下一页
"""
for page in range(1, 11):
URL = f'https://s.taobao.com/search?commend=all&ie=utf8&initiative_id=tbindexz_20170306&localImgKey=&page={page}&q=%E6%89%8B%E6%9C%BA&search_type=item&sourceId=tb.index&spm=a21bo.jianhua%2Fa.201856.d13&ssid=s5-e&tab=all'
borser.get(url=URL)
time.sleep(5)
name_list = borser.find_elements(By.CSS_SELECTOR, '#pageContent > div.LeftLay--leftWrap--xBQipVc > div.LeftLay--leftContent--AMmPNfB > div.Content--content--sgSCZ12 > div > div')
for i in name_list:
name = i.find_element(By.CSS_SELECTOR, 'div > a > div > div.ShopInfo--shopInfo--ORFs6rK > div.ShopInfo--TextAndPic--yH0AZfx > a').text
print(name)
print(f"这是第{page}次")
# borser.quit()
05-25