【发布时间】:2021-03-30 23:46:52
【问题描述】:
我是程序和网络抓取的新手。我想编写一个程序来访问一些最畅销的游戏并提取评论内容,但是当它试图访问 M 级游戏时,我的程序只是被重定向到年龄检查页面。 因此,我使用 selenium 单击一些按钮,以便通过年龄检查。
这是一个例子:
IDlist = ['730','1085660']
chrome = webdriver.Chrome('./chromedriver')
for i in IDlist:
url = 'https://steamcommunity.com/app/%s/reviews/?l=english&browsefilter=toprated&snr=1_5_100010_' %i
chrome.get(url)
#the age check page
chrome.find_element_by_css_selector('#ViewAllForApp').click()
chrome.find_element_by_css_selector('#age_gate_btn_continue').click()
for x in range(1,5):
chrome.execute_script('window.scrollTo(0,document.body.scrollHeight);')
time.sleep(2)
soup = BeautifulSoup(chrome.page_source, 'html.parser')
.
.
.
第一个游戏 (ID=730) 可以运行,但问题是当我访问第二个游戏 (ID=1085660) 时,它没有年龄检查页面和以下错误消息:
NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"#ViewAllForApp"}
(Session info: chrome=87.0.4280.88)
如何避免这个问题? if-else?
或者尝试其他方法通过年龄检查?(例如添加cookies)
【问题讨论】:
标签: python selenium selenium-webdriver web-scraping beautifulsoup