带有硒元素的访问被拒绝页面



我试图编写一个小型应用程序,该应用程序使用footlocker并从网站上抓取某些页面。现在,我想做这个应用程序的方法是从footlocker主页开始,然后点击网站上的不同部分。下面我举了一个例子,其中一个额外的链接,我会点击然后抓取。但我遇到的问题是,当应用程序找到按钮并点击它时,我会转到一个错误页面,然后如果我刷新页面,我会得到一个拒绝访问的页面。如果有人能帮我解决这个问题,我将不胜感激。我认为可能是这个问题的一个想法是必须处理cookie,但我在基于网络的应用程序方面经验不足,不知道是否是这样。

webpage = r"http://www.footlocker.com/" 
driver = webdriver.Chrome(r'C:UserssalehDownloadschromedriver_win32chromedriver.exe')
driver.get(webpage)
driver.find_elements_by_xpath("//*[contains(text(), 'Sitemap')]")[0].click()

尝试模拟普通浏览器的功能:

添加页眉

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opts = Options()
# Add headers
user_agent =  ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) '
'AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/39.0.2171.95 Safari/537.36')
opts.add_argument(f'user-agent={user_agent}')
# Remove the Automation Info 
opts.add_argument('--disable-infobars')
# if you move chromedriver.exe into C:Windows or C:Userssaleh or location where this code is executed, then you don’t have to pass it here
chrome_exe = r'C:UserssalehDownloadschromedriver_win32chromedriver.exe'
driver = webdriver.Chrome(chrome_exe, chrome_options=opts)
URL不应代表r
webpage = 'http://www.footlocker.com/'
driver.get(webpage)

观察你所看到的。打开开发人员工具,首先手动执行该步骤,同时观察正在交互的元素。然后编写代码来执行相同的步骤。

最新更新