Python 如何访问一个div中的特定链接?

Python 如何访问一个div中的特定链接?,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我正在尝试访问该div中的第二个链接(该div中有许多链接): 这里的目标是将link7分配给“/stats/teams/matches/7865/HAVU” 如果需要更多信息,请告诉我。您可以获取所有元素,并使用get()方法提取href属性。然后从结果列表中获取第二个链接。可以使用列表理解在一行中完成: link7 = [a.get("href") for a in teammatch[0].find_all("a")][1] 您要从哪个url抓取的初始完整开始url? r = soup.

我正在尝试访问该div中的第二个链接(该div中有许多链接):

这里的目标是将link7分配给“/stats/teams/matches/7865/HAVU”


如果需要更多信息,请告诉我。

您可以获取所有
元素,并使用
get()
方法提取href属性。然后从结果列表中获取第二个链接。可以使用列表理解在一行中完成:

link7 = [a.get("href") for a in teammatch[0].find_all("a")][1]

您要从哪个url抓取的初始完整开始url?
r = soup.find_all('div',{'class':'tab-content '},{'id':'TODAY'})

for result in r[1:]:
    tournament = result.find_all('div',{'class':'ongoing-event-holder'})
    link = tournament.find('a')['href']

link2 = requests.get('https://www.hltv.org' + link, headers=headers)
soup2 = BeautifulSoup(link2.text,'html.parser')
team = soup2.find_all('div',{'class':'groups'})
for result1 in team:
    team1 = result1.find('div',{'class':'text-ellipsis'})
    link3 = team1.find('a')['href']

link4 = requests.get('https://www.hltv.org' + link3, headers=headers)
x = ('https://www.hltv.org' + link3) + str('#tab-statsBox')
link5 = requests.get(x, headers=headers)
soup3 = BeautifulSoup(link5.text, 'html.parser')
team_stats = soup3.find_all('div',{'class':'moreButton-container'})
for result2 in team_stats:
    teamstatlink = result2.find('a')['href']

link6 = requests.get('https://www.hltv.org' + teamstatlink, headers=headers)
soup4 = BeautifulSoup(link6.text, 'html.parser')
team_matches = soup4.find_all('div',{'class':'stats-top-menu'})
for result3 in team_matches:
    teammatch = result3.find_all('div',{'class':'tabs standard-box'})
link7 = [a.get("href") for a in teammatch[0].find_all("a")][1]