我正试图在https://nyaa.si/的首页上搜索torrent名称和torrent磁铁链接。我成功地获得了磁石链接,但我对torrent名称有问题。这是由于torrent名称所在位置的HTML结构造成的。我试图抓取的内容位于<td>
标记(表行)中,该标记可以通过属性进行唯一标识,但在此之后,内容位于<title>
属性下的<a>
标记中,我看不到唯一可标识的属性。有什么方法可以帮我搜集这些信息吗
这是我的密码:
import re, requests
from bs4 import BeautifulSoup
nyaa_link = 'https://nyaa.si/'
request = requests.get(nyaa_link, headers={'User-Agent': 'Mozilla/5.0'})
source = request.content
soup = BeautifulSoup(source, 'lxml')
#GETTING TORRENT NAMES
title = []
rows = soup.findAll("td", colspan="2")
for row in rows:
title.append(row.content)
#GETTING MAGNET LINKS
magnets = []
for link in soup.findAll('a', attrs={'href': re.compile("^magnet")}):
magnets.append(link.get('href'))
print(magnets)
您需要从表基准中的链接中提取标题。因为这里的每个
<td>
都包含一个<a>
,所以只需调用td.find('a')['title']
所以我已经解决了问题,找到了解决办法
问题是这一行:
if 'comment' not in desired_title:
它只处理不包含“注释”的HTML。问题是我试图抓取页面上HTML结构的方式,基本上,如果torrent对它有评论,它将显示在HTML结构上,高于标题名。因此,我的代码将完全跳过带有注释的torrents
以下是一个可行的解决方案:
感谢CannedScientist提供解决方案所需的一些代码
相关问题 更多 >
编程相关推荐