如何解决使用 fake_user 代理,元素未导入
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(r'c:\chromedriver\chromedriver.exe',options=options)
url = f'https://www.google.com/search?q=ipad+"apple"+intext:apple&hl=en&rlz=&start=10'
driver.get(url)
items = driver.find_elements_by_xpath('//*[@id="search"]/div/div/div')
title = driver.find_elements_by_xpath('//*[@id="search"]/div/div/div/div/div/div/a/h3')
print(len(items))
print(len(title))
for item in items:
title = item.find_element_by_xpath('.//div/div/div/a/h3').text
print(title)
输出,有效
添加 fake_user 代理如下,
from selenium import webdriver
from fake_useragent import UserAgent
options = webdriver.ChromeOptions()
options.add_argument('headless')
ua = UserAgent()
userAgent = ua.random
print(userAgent)
options.add_argument(f'user-agent={userAgent}')
driver = webdriver.Chrome(r'c:\chromedriver\chromedriver.exe',options=options)
url = f'https://www.google.com/search?q=ipad+"apple"+intext:apple&hl=en&rlz=&start=10'
driver.get(url)
items = driver.find_elements_by_xpath('//*[@id="search"]/div/div/div')
title = driver.find_elements_by_xpath('//*[@id="search"]/div/div/div/div/div/div/a/h3')
print(len(items))
print(len(title))
for item in items:
title = item.find_element_by_xpath('.//div/div/div/a/h3').text
print(title)
添加 fake_user 代理代码后,为什么我不能从相同的代码中得到结果?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。