【发布时间】:2022-01-14 13:28:51
【问题描述】:
我正在尝试自动从公共网站下载字幕。单击下载链接(西班牙语为Descargar)后,即可访问字幕。 检查网站的代码,我可以看到链接是jQuery事件:
我猜这个事件中有一个函数处理下载(我对 JS 一点也不熟悉):
function(a) {
if (ajaxflagon()) return !1;
var r = $(this).attr("rel");
if (r = r.split(","), 3 == r.length) var e = "/updated/" + r[0] + "/" + r[1] + "/" + r[2];
else var e = "/original/" + r[0] + "/" + r[1];
ga("send", "pageview", "/" + e, {
title: "Descargando " + $(this).attr("title")
}), $(this).attr("href", e), ajaxflagoff()
}
到目前为止,我的代码可以找到正确的链接:
import urllib.request as urlRequest
from bs4 import BeautifulSoup
# Subtitles for a specific TV show
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"):
# only interested in the spanish language
if "Español (España)" in str(lang):
for element in lang.parent.findAll("a", class_="bt_descarga"):
results.append(element)
缺少的是下载部分:( 我该怎么做?
提前致谢。
【问题讨论】:
-
beautifulsoup 它纯粹是一个解析 html 的包。它绝不与浏览器交互。为此,您需要使用 Selenium,或者查看是否有直接 url 可以从请求中获取该数据。
-
如果你转到
href中的那个链接,那会返回你想要的吗?
标签: python beautifulsoup