python - bs4 下载文件甚至 jQuery 点击

标签 python beautifulsoup

我正在尝试自动从公共(public)网站下载字幕。单击下载链接(西类牙语为 Descargar)后即可访问字幕。 检查网站的代码,我可以看到链接是 jQuery 事件:

enter image description here

我猜这个事件中有一个函数处理下载(我对 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)

它缺少的是下载部分:( 我该怎么做?

提前致谢。

最佳答案

您可以在 Python 中实现该 JS 事件函数并创建下载 URL

最后,您可以使用 URL 下载字幕。

以下是仅获取西类牙语订阅者的方法:

from shutil import copyfileobj

import requests
from bs4 import BeautifulSoup

base_url = "https://www.tusubtitulo.com"
season = "/season/4674/1"

headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:95.0) Gecko/20100101 Firefox/95.0",
    "Referer": f"{base_url}{season}",
}


def get_rel_attributes(page: str):
    return [
        a["rel"][0] for a in
        BeautifulSoup(page, "lxml").select(".bt_descarga")[1::3]  # this gets only the Spanish subs
    ]


# This is the JS function translated to Python that's responsible for
# building the subtitle download urls.
def get_download_urls(rel_attributes: list):
    src_urls = []
    for item in rel_attributes:
        elements = item.split(",")
        one, two, three = elements
        if len(elements) == 3:
            src_urls.append(f"{base_url}/updated/{one}/{two}/{three}")
        else:
            src_urls.append(f"{base_url}/original/{one}/{two}")
    return src_urls


def downloader(target_url: str, conn: requests.Session):
    response = conn.get(target_url, headers=headers, stream=True)
    file_name = (
        response.headers["Content-Disposition"]
        .split("=", -1)[-1]
        .replace('"', "")
        .encode('latin-1')
        .decode('utf-8')
    )
    print(f"Fetching {file_name}...")
    with open(file_name, "wb") as output:
        copyfileobj(response.raw, output)


if __name__ == "__main__":
    with requests.Session() as connection:
        source_page = connection.get(f"{base_url}{season}", headers=headers).text

    for url in get_download_urls(get_rel_attributes(source_page)):
        downloader(url, connection)

您应该在运行脚本的文件夹中看到此输出和 10 个文件:

Fetching Invasion (2021) 1x01 - Last Day (Español (España)).srt...
Fetching Invasion (2021) 1x02 - Crash (Español (España)).srt...
Fetching Invasion (2021) 1x03 - Orion (Español (España)).srt...
Fetching Invasion (2021) 1x04 - The King is Dead (Español (Latinoamérica)).srt...
Fetching Invasion (2021) 1x05 - Going Home (Español (España)).srt...
Fetching Invasion (2021) 1x06 - Home Invasion (Español (España)).srt...
Fetching Invasion (2021) 1x07 - Hope (Español (España)).srt...
Fetching Invasion (2021) 1x08 - Contact (Español (España)).srt...
Fetching Invasion (2021) 1x09 - Full of Stars (Español (España)).srt...
Fetching Invasion (2021) 1x10 - First Day (Español (España)).srt...

关于python - bs4 下载文件甚至 jQuery 点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70711307/

相关文章:

python - 这个条件运算符有什么作用?

python - Python 中关于 mlpy.dtw 包的两个问题?

python - 如何删除 pymongo.Database.Database 对象

python - 从下拉列表中抓取值

python - 如何使用递归在 BeautifulSoup 中进行抓取?

python - 用 beautifulsoup 提取属性值

python - zip 变量首次使用后为空

python - socket.getfqdn() 不返回域,但 socket.gethostname() 返回域吗?

python - 你能解释一下这个 python 对方括号的使用吗?

python - 如何使用漂亮的汤在 XHTML 中提取没有样式键的内联 CSS 样式