我需要從match_items 中獲取所有href,我的代碼:
url_news = "https://www.hltv.org/matches"
response = requests.get(url_news)
soup = BeautifulSoup(response.content, "html.parser")
match_info = []
match_items = soup.find("div", class_="upcomingMatchesSection")
match_info.append(match_items.findAll("a", class_="match a-reset", href=True).item['href'])```
uj5u.com熱心網友回復:
此代碼從 match_items div 生成一個 href 串列。每個 href 都以 '/matches/' 為前綴,我相信這是您所追求的。
url_news = "https://www.hltv.org/matches"
response = requests.get(url_news)
soup = BeautifulSoup(response.content, "html.parser")
match_items = soup.find("div", {"class": "upcomingMatchesSection"})
match_info = [item["href"] for item in match_items.findAll("a", {"class": "match a-reset"})]
uj5u.com熱心網友回復:
您可以使用上一個問題的結果或使用以下代碼行獲取結果:
[link['href'] for link in soup.select("div.upcomingMatch a")]
它選擇所有<div>包含<a>和串列理解語法迭代所有結果以創建帶有 url 的串列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/314392.html
