Python 在网页间循环浏览网页
我正在尝试访问此网站上的数据:。到目前为止,我的代码在下拉菜单中循环,我想循环表[1][2]顶部的页面。。发射型计算机断层扫描仪。我曾尝试使用Select,但出现一个错误,Select无法与span一起使用:“UnexpectedTagNameException:Select仅对元素有效,而对无效” 这就是我在选择页面时遇到的问题:Python 在网页间循环浏览网页,python,selenium,web-scraping,Python,Selenium,Web Scraping,我正在尝试访问此网站上的数据:。到目前为止,我的代码在下拉菜单中循环,我想循环表[1][2]顶部的页面。。发射型计算机断层扫描仪。我曾尝试使用Select,但出现一个错误,Select无法与span一起使用:“UnexpectedTagNameException:Select仅对元素有效,而对无效” 这就是我在选择页面时遇到的问题: change_page=Select(driver.find_element_by_class_name("yui-pg-pages"))
change_page=Select(driver.find_element_by_class_name("yui-pg-pages"))
page_count = len(change_page.options)
for k in range(1, page_count):
change_page.select_by_index(k)
# Select Page & run following code
soup = BeautifulSoup(driver.page_source, 'html.parser')
print(soup.find_all("tbody", {"class" : re.compile(".*")})[1])
# get the needed table body
table=soup.find_all("tbody", {"class" : re.compile(".*")})[1]
rows = table.find_all('tr')
for row in rows:
cols = row.find_all('td')
cols = [ele.text.strip() for ele in cols]
data.append(cols)
改用xpath选择器
driver.find_element_by_xpath('//a[@class="yui-pg-next"]')
然后只需循环,同时您可以与“下一步”按钮交互。如果在循环浏览页面时页面的数量可以改变,我更喜欢这种方法。您不需要使用选择。事实上,我不认为Select
只适用于下拉菜单
或者,如果因为页面不经常更改而需要使用页面链接,您可以尝试以下方法:
# Use find_elements_by_xpath to select multiple elements.
pages = driver.find_elements_by_xpath('//a[@class="yui-pg-page"]')
# loop through results
for page_link in pages:
page_link.click()
# do stuff.
是的,非常感谢!你应该通过点击答案旁边的复选标记来接受答案,这将真正帮助我:D
# Use find_elements_by_xpath to select multiple elements.
pages = driver.find_elements_by_xpath('//a[@class="yui-pg-page"]')
# loop through results
for page_link in pages:
page_link.click()
# do stuff.