【发布时间】:2018-05-09 12:43:33
【问题描述】:
我想从网页上特定标题下的所有链接中提取所有表格。
代码应该能够转到特定标题并从其中的所有链接加载所有表格。
【问题讨论】:
标签: python pandas selenium selenium-webdriver css-selectors
我想从网页上特定标题下的所有链接中提取所有表格。
代码应该能够转到特定标题并从其中的所有链接加载所有表格。
【问题讨论】:
标签: python pandas selenium selenium-webdriver css-selectors
我想我误解了您想要的内容。.text 会为您提供 a 标签内的单词。 <a href="http://url">this is what it would get</a>。
如果您想要实际的链接,那么您就在正确的轨道上,但是您的元素太宽泛了。
好吧,从我一直在谈论的内容来看,您的代码有一些问题:
chrome,然后又称为browser。您只需要其中之一。\。所有 Windows 路径都应该有 \\ 而不是所有的反斜杠。现在您的 elems 代码.. 您已经列出了很多东西,您应该每行获取一组元素并使用检查器浏览页面代码。
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])
要导航链接,您必须将驱动程序(您称之为browser)发送给他们。如果它变得更复杂,我会使用一个函数,您可以将browser 作为变量传递。
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()
一旦你在这个过程中重复..首先检查页面。用同样的方法获取你想要的元素。
【讨论】: