【发布时间】:2022-01-10 20:23:13
【问题描述】:
这是我第一次尝试用 beautifulsoup 从网上抓取一些字幕。这离我的目标还很远,但就目前而言,我只是想实现这一点(我正在慢慢学习:D)
我只对这个“tr”对象中的一种语言感兴趣。
下面的代码获取所有字幕对象,因为我不知道如何在“tr”对象中过滤我的选择:
import urllib.request as urlRequest
from bs4 import BeautifulSoup
urlpage = 'https://www.tusubtitulo.com/season/4674/1'
# pretend to be a chrome 47 browser on a windows 10 machine
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)"
req = urlRequest.Request(urlpage, headers=headers)
# open the url
url = urlRequest.urlopen(req)
# get the source code
source_code = url.read()
# parse the html using beautiful soup and store in variable 'soup'
soup = BeautifulSoup(source_code, 'html.parser')
results = []
for lang in soup.findAll("td", class_="language"):
if "Español (España)" in str(lang):
for element in soup.findAll("a", class_="bt_descarga"):
results.append(element)
results
【问题讨论】:
-
即使您过滤掉西班牙语字幕,您也不会得到
href,因为那里有一个JS事件监听器。换句话说,没有指向 HTML 源文件的链接。 -
没错。但这将是“未来”的一个问题,这就是为什么我说它还远未完成:D
标签: python beautifulsoup urllib