今天給你們帶來了使用 mechanicalsoup 爬取電影天堂(https://www.dytt.to)中電影名,電影,以及磁力鏈接的實例
import mechanicalsoup
from urllib.parse import urljoin, unquote
#創建Browser對象
browser = mechanicalsoup.Browser()
#訪問電影天堂歐美電影
url = "https://www.dytt.to/html/gndy/oumei/"
page = browser.get(url)
#獲取電影名
links = page.soup.select(".ulink")
for link in links:
text = link.text.strip()
#獲取鏈接
href = link.get("href")
full_url = urljoin(url, href)
#訪問電影鏈接
movie_page = browser.get(full_url)
#獲取<div id="Zoom">標籤
zoom_div = movie_page.soup.find("div", {"id": "Zoom"})
#如果zoom_div存在,則獲取<a>標籤的href值
if zoom_div:
a_tags = zoom_div.find_all("a")
for a_tag in a_tags:
a_href = unquote(a_tag.get("href"))
print(f"電影名:{text}\n鏈接:{full_url}")
print(f"磁力下載鏈接:{a_href}\n\n")
print("-----------------")
輸出:
電影名:2023 年劇情驚悚《沼澤王的女兒》BD 中英雙字
鏈接:https://www.dytt.to/html/gndy/dyzz/20231123/64365.html
磁力下載鏈接:magnet:?xt=urn:btih&dn = 陽光電影 dygod.org. 沼澤王的女兒.2023.BD.1080P. 中英雙字.mkv&tr=udp://tracker.opentrackr.org:1337/announce&tr=udp://exodus.desync.com:6969/announce
電影名:2023 年動畫喜劇《蜥蜴伯伯里奧》BD 中英雙字
鏈接:https://www.dytt.to/html/gndy/dyzz/20231123/64363.html
磁力下載鏈接:magnet:?xt=urn:btih:699620e20227fcebbbaac4f6a76988ee59354cc1&dn = 陽光電影 dygod.org. 蜥蜴伯伯里奧.2023.BD.1080P. 中英雙字.mkv&tr=udp://tracker.opentrackr.org:1337/announce&tr=udp://exodus.desync.com:6969/announce
電影名:2023 年動作《約翰尼與克萊德》BD 英語中字
鏈接:https://www.dytt.to/html/gndy/jddy/20231122/64362.html
磁力下載鏈接:magnet:?xt=urn:btih:61b765ce57d2aaa58fccc8ff2b42da9445618a81&dn = 陽光電影 dygod.org. 約翰尼與克萊德.2023.BD.1080P. 英語中字.mkv&tr=udp://tracker.opentrackr.org:1337/announce&tr=udp://exodus.desync.com:6969/announce
電影名:2023 年劇情《盲證》BD 中英雙字
鏈接:https://www.dytt.to/html/gndy/jddy/20231121/64359.html
磁力下載鏈接:magnet:?xt=urn:btih:625c90c6d1b218a338c03ec4b3fea01c3f9fde9a&dn = 陽光電影 dygod.org. 盲證.2023.BD.1080P. 中英 雙字.mkv&tr=udp://tracker.opentrackr.org:1337/announce&tr=udp://exodus.desync.com:6969/announce
電影名:2023 年恐怖《驅魔人:信徒》BD 英語中字
鏈接:https://www.dytt.to/html/gndy/jddy/20231120/64357.html
磁力下載鏈接:magnet:?xt=urn:btih&dn = 陽光電影 dygod.org. 驅魔人:信徒.2023.BD.1080P. 英語中字.mkv&tr=udp://tracker.opentrackr.org:1337/announce&tr=udp://exodus.desync.com:6969/announce
電影名:2023 年劇情驚悚《男氣管制》BD 中英雙字
鏈接:https://www.dytt.to/html/gndy/jddy/20231119/64356.html
磁力下載鏈接:magnet:?xt=urn:btih:94c703906554074e3e90f9d36d839a75f3845209&dn = 陽光電影 dygod.org. 男氣管制.2023.BD.1080P. 中英雙字.mkv&tr=udp://tracker.opentrackr.org:1337/announce&tr=udp://exodus.desync.com:6969/announce
電影名:2023 年劇情驚悚《男氣管制》BD 中英雙字
鏈接:https://www.dytt.to/html/gndy/jddy/20231119/64356.html
磁力下載鏈接:magnet:?xt=urn:btih:94c703906554074e3e90f9d36d839a75f3845209&dn = 陽光電影 dygod.org. 男氣管制.2023.BD.1080P. 中英雙字.mkv&tr=udp://tracker.opentrackr.org:1337/announce&tr=udp://exodus.desync.com:6969/announce
電影名:2023 年劇情驚悚《男氣管制》BD 中英雙字
鏈接:https://www.dytt.to/html/gndy/jddy/20231119/64356.html
磁力下載鏈接:magnet:?xt=urn:btih:94c703906554074e3e90f9d36d839a75f3845209&dn = 陽光電影 dygod.org. 男氣管制.2023.BD.1080P. 中英雙字.mkv&tr=udp://tracker.opentrackr.org:1337/announce&tr=udp://exodus.desync.com:6969/announce