总的来说,我对编程非常陌生,我正在尝试编写我自己的小程序。我正在使用优美汤,以提取标题和磁铁链接的洪流文件。但是,find()元素始终不返回,无论我做什么。这一页是正确的。我还用find_next_sibling进行了测试,并阅读了所有类似的问题,但都没有结果。由于没有错误,我不知道我的错误是什么。任何帮助都将不胜感激。下面是我的代码:
import urllib3
from bs4 import BeautifulSoup
print("Please enter the movie name: \n")
search_string = input("")
search_string.rstrip()
search_string.lstrip()
open_page = ('https://www.yify-torrent.org/search/' + search_string + '/s-1/all/all/') # get link - creates a search string with input value
print(open_page)
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
manager = urllib3.PoolManager(10)
page_content = manager.urlopen('GET',open_page)
soup = BeautifulSoup(page_content,'html.parser')
magnet = soup.find('a', attrs={'class': 'movielink'}, href=True)
print(magnet)发布于 2018-11-30 19:47:41
看看下面的脚本,它做的正是你想要达到的目标。我使用的是requests库而不是urllib3。您所犯的主要错误是在错误的位置查找magnet链接。你需要深入一层才能挖掘出这个链接。尝试使用quote而不是字符串操作来适应url中的搜索查询。
试试看:
import requests
from urllib.parse import urljoin
from urllib.parse import quote
from bs4 import BeautifulSoup
keyword = 'The Last Of The Mohicans'
url = 'https://www.yify-torrent.org/search/'
base = f"{url}{quote(keyword)}{'/p-1/all/all/'}"
res = requests.get(base)
soup = BeautifulSoup(res.text,'html.parser')
tlink = urljoin(url,soup.select_one(".img-item .movielink").get("href"))
req = requests.get(tlink)
sauce = BeautifulSoup(req.text,"html.parser")
title = sauce.select_one("h1[itemprop='name']").text
magnet = sauce.select_one("a#dm").get("href")
print(f"{title}\n{magnet}") https://stackoverflow.com/questions/53561925
复制相似问题