【发布时间】:2020-04-02 22:46:18
【问题描述】:
关于 StackOverFlow 的第一个问题。我正在尝试网络抓取fxstreet.com/news。似乎他们的新闻提要正在动态生成文章。 BeautifulSoup 无法收集这些信息,所以我决定使用 Selenium。但是,我无法使用 Selenium 访问显示的文章。
import requests
from bs4 import BeautifulSoup
import re
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://www.fxstreet.com/news?q=&hPP=17&idx=FxsIndexPro&p=0')
article = driver.find_element_by_link_text('/news')
for post in article:
print(post.text)
我想制作一个定期检查新文章的爬虫,这些文章的 URL 为:https://www.fxstreet.com/news...(endpoint)。
但是,当我尝试查找 hrefs/'a' 标签时,我会在整个网站上获得许多链接,但它们都不是实时提要中的新闻文章。当我查找每个“div”时,我会为我准备好整个 html:
<article class="fxs_entriesList_article_with_image ">
<h3 class="fxs_entryHeadline">
<a href="https://www.fxstreet.com/news/gbp-usd-upside-potential-limited-in-covid-19-uncertainties-202004021808" title="GBP/USD upside potential limited in COVID-19 uncertainties">GBP/USD upside potential limited in COVID-19 uncertainties</a>
</h3>
<address class="fxs_entry_metaInfo">
<span class="fxs_article_author">
By <a href="/author/ross-j-burland" rel="nofollow">Ross J Burland</a>
</span> | <time pubdate="" datetime="">18:08 GMT</time>
</address>
</article>
告诉我它以某种方式存在于某个地方,但我完全无法与之互动。那么,当 Selenium 无法搜索“a”标签或部分链接时,如何访问我需要的链接?我还尝试使用以下方法查找确切的链接:
elem = driver.find_elements_partial_link("news")
for element in elem:
print(element.get_attribute("innerHTML"))
无济于事。我也尝试过显式和隐式等待。谢谢。
【问题讨论】:
标签: python selenium web-scraping