我試圖通過這個網頁來獲取所有與蘋果有關的新聞文章的鏈接。https://finance.yahoo.com/quote/AAPL/news?p=AAPL。但是中間也有很多廣告的鏈接,還有其他引導到網站其他頁面的鏈接。我如何有選擇地只獲取新聞文章的鏈接? 以下是我到目前為止寫的代碼:
driver = webdriver.Chrome(executable_path='C:UsersHomeOneDriveDesktopAJchromedriver_win32chromedriver.exe)
driver.get("https://finance.yahoo.com/quote/AAPL/news?p=AAPL")
鏈接=[]
for a in driver.find_elements_by_xpath(' ./a'):
links.append(a.get_attribute('href'))
def get_info(url)。
#send request: 回應 = requests.get(url)
回應 = requests.get(url)
#parse #get information we need
news = soup.find('div'/span>, attrs={'class'/span>: 'caas-body'}).text
headline = soup.find('h1').text
日期 = soup.find('time').text
return新聞、標題、日期
有誰能指導如何做到這一點,或者有什么資源可以幫助到這一點?謝謝!
uj5u.com熱心網友回復:
試試這個xpath來獲取該頁面的所有新聞鏈接。
//li[contains(@class,'js-stream-content')]/div[@data-test-locator='mega']//h3/a
driver.implicitly_wait(10)
driver.maximum_window()
driver.get("https://finance.yahoo.com/quote/AAPL/news?p=AAPL")
time.sleep(10)
links = driver.find_elements_by_xpath("//li[contains(@class,'js-stream-content')]/div[@data-test-locator='mega']//h3/a"/span>)
for link in links:
print(link.get_attribute("href"/span>)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/327096.html
標籤:
