为何dlink_find('a')['href']无法生效?YIFY字幕下载脚本问题
Fixing the
dlink_find('a')['href'] Issue in YIFY Subtitles Scraper It looks like your code is stuck at accessing the download link because you haven’t fully navigated to the subtitle detail page yet. The search results page only lists movie entries—you need to first visit each movie’s subtitle page to get the actual download links. Here’s how to fix this step by step:
Step 1: Extract the Subtitle Page URL from Search Results
First, in your loop over media-body divs, you need to grab the link to the movie’s subtitle page. Each media-body contains an <a> tag pointing to this page. Let’s adjust that part:
import requests from bs4 import BeautifulSoup count = 0 usearch = input("Movie Name? : ") search_url = f"https://www.yifysubtitles.com/search?q={usearch}" base_url = "https://www.yifysubtitles.com" print(search_url) resp = requests.get(search_url) soup = BeautifulSoup(resp.content, 'lxml') # Loop through each movie result in the search results for movie_card in soup.find_all("div", class_="media-body"): # Get the link to the subtitle page and build the full URL try: subtitle_page_link = movie_card.find('a')['href'] full_subtitle_url = base_url + subtitle_page_link print(f"Visiting subtitle page: {full_subtitle_url}") except TypeError: print("Skipping entry: No subtitle page link found") continue # Fetch the content of the subtitle page subtitle_resp = requests.get(full_subtitle_url) subtitle_soup = BeautifulSoup(subtitle_resp.content, 'lxml') ### Step 2: Locate the Download Link on the Subtitle Page # Target high-rated subtitles (you can adjust this selector based on your needs) for subtitle_entry in subtitle_soup.find_all("tr", class_="high-rating"): download_cell = subtitle_entry.find("td", class_="download-cell") if download_cell: try: download_link = download_cell.find('a')['href'] full_download_url = base_url + download_link print(f"Found download URL: {full_download_url}") ### Step 3: Download and Save the Subtitle File subtitle_file = requests.get(full_download_url) # Save the zip file (customize the filename as needed) with open(f"{usearch}_subtitle_{count+1}.zip", 'wb') as f: f.write(subtitle_file.content) count += 1 print(f"Subtitle {count} downloaded successfully!") except TypeError: print("Skipping entry: No download link found") continue
Key Fixes & Explanations:
- Navigate to Subtitle Pages: Search results don’t have direct download links—you must visit each movie’s subtitle page first. We use
movie_card.find('a')['href']to get that link and combine it with the base URL. - Robust Element Selection: Added try-except blocks to handle cases where elements (like links) aren’t found, preventing your script from crashing unexpectedly.
- Target Relevant Subtitles: The
high-ratingclass filters for better-quality subtitles, but you can adjust this selector (e.g., look for specific languages) based on your needs.
Important Notes:
- Always check the website’s Terms of Service before scraping—some sites prohibit automated access.
- Website structures change over time, so you may need to update class names or selectors if this script stops working later.
- Add a small delay between requests (using
time.sleep(2)for example) to avoid overwhelming the server.
内容的提问来源于stack exchange,提问作者Aakash Hirve
相关产品推荐
相关产品推荐

