Python 如何在selenium中的特定标题下打印每个链接中的表格?
我想从网页上特定标题下的所有链接中提取所有表格Python 如何在selenium中的特定标题下打印每个链接中的表格?,python,pandas,selenium,selenium-webdriver,css-selectors,Python,Pandas,Selenium,Selenium Webdriver,Css Selectors,我想从网页上特定标题下的所有链接中提取所有表格 代码应该能够转到特定的标题,并从其中的所有链接加载所有表。我想我误解了您想要的内容.text将获取a标记中的单词。 如果你想要实际的链接,你就在正确的轨道上,但是你的元素太广泛了 好的,根据我刚才所说的,您的代码有一些错误: 您将webdriver称为chrome,然后又称为browser。你只需要一个 您的路径中有一个空的\。所有Windows路径都应该有\\,而不是所有反斜杠 现在你的元素代码。。您已经列出了很多内容,每行应该有一组元素,并
代码应该能够转到特定的标题,并从其中的所有链接加载所有表。我想我误解了您想要的内容
.text
将获取a标记中的单词<代码>。
如果你想要实际的链接,你就在正确的轨道上,但是你的元素太广泛了
好的,根据我刚才所说的,您的代码有一些错误:
- 您将webdriver称为
,然后又称为chrome
。你只需要一个browser
- 您的路径中有一个空的
。所有Windows路径都应该有\
,而不是所有反斜杠\\
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
browser = webdriver.Chrome(
'C:\\Users\\chromedriver.exe',
chrome_options=chrome_options)
browser.get(
"https://www.juniper.net/support/eol/") # <-- Sample Website here
time.sleep(1)
# Get the ul linklist elements, and all the links in them.
# So you can see how you could use this to narrow it down further.
lists = browser.find_elements_by_css_selector("ul.linkList") <-- Sample css_selector for the heading you want to extract from
links = [link.find_elements_by_tag_name("a") for link in lists]
# Now in links we have a list for each column element
# You can use indexing like links[0] to only select one column
for elems in links:
print([link.get_attribute("href") for link in elems])
一旦你进入了这个过程,它就会重复。。首先检查页面。使用相同的方法获取所需的元素。谢谢您的回答,我得到错误“NoneType”对象没有属性“text”,我需要提取这些链接中的表,因此如果您有其他建议,请告诉我基本上我要提取这些链接中的表。按标签名称(“a”)查找元素这将提取所有链接,而不是标题下的特定链接,这是因为它到达列表的末尾并且没有终止,然后出现此错误
tables = []
for elems in links:
tables += [link.get_attribute("href") for link in elems]
for link in tables:
browser.get(link)
table = browser.find_elements_by_tag_name("td")
if table:
table_rows = [t.find_elements_by_tag_name("tr") for t in table]
for table_row in table_rows:
your_result = [t.text for t in table_row if not t.startswith("Pages")]
if your_result:
print(your_result)
browser.close()
browser.quit()