我怎样才能只从 soup.find 而不是 find_all 获取所有 href
How can i get all hrefs only from soup.find not find_all
我需要从 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'])```
此代码从 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"})]
您可以使用 的结果或通过以下代码行获取:
[link['href'] for link in soup.select("div.upcomingMatch a")]
它正在选择包含 <a>
的所有 <div>
并使用列表理解语法迭代所有结果以创建包含 url 的列表。
我需要从 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'])```
此代码从 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"})]
您可以使用
[link['href'] for link in soup.select("div.upcomingMatch a")]
它正在选择包含 <a>
的所有 <div>
并使用列表理解语法迭代所有结果以创建包含 url 的列表。