Python
运行代码复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
import time
# 设置代理信息
proxyHost = "www.16yun.cn"
proxyPort = "5445"
proxyUser = "16QMSOML"
proxyPass = "280651"
# 设置代理认证的脚本
proxy_auth_plugin_path = 'path/to/proxy_auth_plugin.zip' # 替换为你的代理插件路径
# 设置浏览器驱动路径
driver_path = 'path/to/chromedriver'
# 初始化 Chrome 选项
chrome_options = Options()
# 添加代理设置
chrome_options.add_argument(f'--proxy-server={proxyHost}:{proxyPort}')
# 添加代理认证插件(如果需要)
chrome_options.add_extension(proxy_auth_plugin_path)
# 初始化 WebDriver
driver = webdriver.Chrome(executable_path=driver_path, options=chrome_options)
# 打开目标电商网站页面
url = 'https://www.taobao.com'
driver.get(url)
# 滚动页面到底部
def scroll_to_bottom():
driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')
# 等待页面加载
def wait_for_loading():
time.sleep(2) # 等待 2 秒,等待页面加载完成
# 滚动翻页爬取数据
for _ in range(5): # 假设滚动 5 次
scroll_to_bottom()
wait_for_loading()
# 获取页面源码
html = driver.page_source
# 使用 BeautifulSoup 解析页面
soup = BeautifulSoup(html, 'html.parser')
# 提取商品信息
products = soup.find_all('div', class_='product-item') # 假设商品信息在 class 为 product-item 的 div 中
for product in products:
title = product.find('h3', class_='product-title').text.strip() # 提取商品标题
price = product.find('span', class_='product-price').text.strip() # 提取商品价格
print(f'商品标题:{title}, 商品价格:{price}')
# 关闭 WebDriver
driver.quit()