我正在尝试从特定链接中抓取一些url,我使用beautiful-soup来抓取那些链接,但我无法抓取那些链接。在这里,我附上我已经使用的代码。实际上,我想从类“ fxs_aheadline_tiny”中抓取网址]
import requests
from bs4 import BeautifulSoup
url = 'https://www.fxstreet.com/news?q=&hPP=17&idx=FxsIndexPro&p=0&dFR%5BTags%5D%5B0%5D=EURUSD'
r1 = requests.get(url)
coverpage = r1.content
soup1 = BeautifulSoup(coverpage, 'html.parser')
coverpage_news = soup1.find_all('h4', class_='fxs_aheadline_tiny')
print(coverpage_news)
谢谢
我会使用硒。请尝试以下代码:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
#open driver
driver= webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://www.fxstreet.com/news?q=&hPP=17&idx=FxsIndexPro&p=0&dFR%5BTags%5D%5B0%5D=EURUSD')
# Use ChroPath to identify the xpath for the 'page hits'
pagehits=driver.find_element_by_xpath("//div[@class='ais-hits']")
# search for all a tags
links=pagehits.find_elements_by_tag_name("a")
# For each link get the href
for link in links:
print(link.get_attribute('href'))
它完全可以满足您的要求:它会删除搜索页面上的所有URL /链接(这也意味着指向作者页面的链接)。
您甚至可以考虑自动化浏览器并浏览搜索页面结果。为此,请参见Selenium文档:https://selenium-python.readthedocs.io/
希望这会有所帮助