gpt4 book ai didi

python - 将 href 字符串转换为链接列表

转载 作者:行者123 更新时间:2023-11-28 00:59:05 25 4
gpt4 key购买 nike

我正在尝试使用以下代码从 Gosugamers 抓取一些统计数据,包括比赛结果和这些比赛的球队名称:

from bs4 import BeautifulSoup
import requests

for i in range(411):
try:
i += 1
print(i)
url = 'http://www.gosugamers.net/counterstrike/gosubet?r-page={}'.format(i)
r = requests.get(url)
web = BeautifulSoup(r.content,"html.parser")
table = web.findAll("table", attrs={"class":"simple matches"})
table = table[1]
links = table('a')
for link in links:
if 'matches' in link.get('href', None):
if len(link.get('href', None)) != 0:
print(link.get('href', None))

except:
pass

但是当我得到 link.get('href', None) 时,它是一个包含所有链接的字符串,用于单个页面上的匹配,我不知道如何把它变成所有链接的列表,如果有人能帮助我,我会很高兴,谢谢!

最佳答案

在我看来,link.get('href', None) 实际上返回了一个链接。 get 方法文档说:

bs4.element.Tag实例的get(self, key, default=None)方法

Returns the value of the 'key' attribute for the tag, or
the value given for 'default' if it doesn't have that
attribute.

因此,当您获得一个包含“匹配项”的链接时,您可以将其添加到列表中。

from bs4 import BeautifulSoup
import requests

all_links = []

i = 1
for i in range(411):
try:
print(i)
url = 'http://www.gosugamers.net/counterstrike/gosubet?r-page={}'.format(i)
r = requests.get(url)
web = BeautifulSoup(r.content,"html.parser")
table = web.findAll("table", attrs={"class":"simple matches"})
table = table[1]
links = table('a')

for link in links:
href = link.get('href')
if href is not None and 'matches' in href:
all_links.append(href)

i += 1
except:
pass

print "Here are all the links: ", all_links

关于python - 将 href 字符串转换为链接列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43062491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com