from selenium import webdriver
from bs4 import BeautifulSoup
import csv
import time
driver = webdriver.Chrome()
url = 'https://www.aqistudy.cn/historydata/monthdata.php?city=%E5%8C%97%E4%BA%AC'
driver.get(url)
# 发现没有数据,因为 默认情况下 代码控制的浏览器 有 很多特征 被检测到了
from selenium.webdriver.chrome.options import Options
# 准备配置
chrome_options = Options()
# chrome_options.add_argument("--headless") 控制不显示窗口
chrome_options.add_argument('user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36')
driver = webdriver.Chrome(options=chrome_options)
# 打开文本文件,得提前有
with open('stealth.min.js') as f:
js = f.read()
# 执行 js 文件代码,去除特征
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": js
})
driver.get(url)
driver.page_source
文件下载:https://download.csdn.net/download/Natalie_Lv/86723031