python - 美丽汤 : Fetched all the links on a webpage how to navigate through them without selenium?

标签 python beautifulsoup

所以我正在尝试编写一个平庸的脚本来从一个特定的网站下载字幕,正如你们所看到的。我是 beautifulsoup 的新手,到目前为止,我有一个搜索查询(GET)后所有“href”的列表。那么,在获得所有链接后,如何进一步导航呢? 代码如下:

import requests
from bs4 import BeautifulSoup

usearch = input("Movie Name? : ")
url = "https://www.yifysubtitles.com/search?q="+usearch
print(url)
resp = requests.get(url)
soup = BeautifulSoup(resp.content, 'lxml')
for link in soup.find_all('a'):
    dictn = link.get('href')
    print(dictn)

最佳答案

您需要使用resp.text而不是resp.content 尝试此操作来获取搜索结果。

import requests
from bs4 import BeautifulSoup

base_url_f = "https://www.yifysubtitles.com"
search_url = base_url_f + "/search?q=last+jedi"
resp = requests.get(search_url)
soup = BeautifulSoup(resp.text, 'lxml')
for media in soup.find_all("div", {"class": "media-body"}):
    print(base_url_f + media.find('a')['href'])

输出:https://www.yifysubtitles.com/movie-imdb/tt2527336

关于python - 美丽汤 : Fetched all the links on a webpage how to navigate through them without selenium?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49378038/

相关文章:

python - 网络抓取新手

python - 如何从变量中获取数据并将其放入另一个变量中

python - 列出 Pandas 数据框 - Python

.jpeg 文件的 Python 导入文件夹

python - 2件套搭配美汤精选

Python BeautifulSoup 使用 re.compile 查找字符串结尾

beautifulsoup - 转换 </br> 到结束行

Python 循环(for 或 while)

python - ConnectionRefusedError at/password-reset/[WinError 10061] 由于目标机器主动拒绝而无法建立连接

python - Django 围绕标题发送电子邮件 u''