Python 美:为什么;dlink#u find(';a';)[';href';];不工作?

Python 美:为什么;dlink#u find(';a';)[';href';];不工作?,python,beautifulsoup,Python,Beautifulsoup,我正在尝试创建一个脚本,从一个特定的网站下载字幕。请阅读代码中的注释。 代码如下: import requests from bs4 import BeautifulSoup count = 0 usearch = input("Movie Name? : ") search_url = "https://www.yifysubtitles.com/search?q="+usearch base_url = "https://www.yifysubtitles.com" print(searc

我正在尝试创建一个脚本,从一个特定的网站下载字幕。请阅读代码中的注释。 代码如下:

import requests
from bs4 import BeautifulSoup

count = 0
usearch = input("Movie Name? : ")
search_url = "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')
for link in soup.find_all("div",{"class": "media-body"}):       #Get the exact class:'media-body'
    imdb = link.find('a')['href']                               #Find the link in that class, which is the exact link we want
    movie_url = base_url+imdb                                   #Merge the result with base string to navigate to the movie page
    print("Movie URL : {}".format(movie_url))                   #Print the URL just to check.. :p

    next_page = requests.get(movie_url)                         #Soup number 2 begins here, after navigating to the movie page
    soup2 = BeautifulSoup(next_page.content,'lxml')
    #print(soup2.prettify())
    for links in soup2.find_all("tr",{"class": "high-rating"}): #Navigate to subtitle options with class as high-rating
        for flags in links.find("td", {"class": "flag-cell"}):  #Look for all the flags of subtitles with high-ratings
            if flags.text == "English":                         #If flag is set to English then get the download link
                print("After if : {}".format(links))
                for dlink in links.find("td",{"class": "download-cell"}):   #Once English check is done, navigate to the download class "download-cell" where the download href exists
                    half_dlink = dlink.find('a')['href']                    #STUCK HERE!!!HERE'S THE PROBLEM!!! SOS!!! HELP!!!
                    download = base_url + half_dlink
                    print(download)
我得到以下错误:

 File "C:/Users/PycharmProjects/WhatsApp_API/SubtitleDownloader.py", line 24, in <module>
    for x in dlink.find("a"):
TypeError: 'NoneType' object is not iterable
文件“C:/Users/PycharmProjects/WhatsApp_API/SubtitleDownloader.py”,第24行,在
对于dlink.find(“a”)中的x:
TypeError:“非类型”对象不可编辑

只需将上面的行更改为:

用于链接中的dlink.find(“td”,“class”:“下载单元”):

为此:

for dlink in links.find_all("td",{"class": "download-cell"}):
因为您在单个元素而不是列表上运行循环

注意:唯一的区别是find_all()返回一个包含单个结果的列表,而find()只返回结果


希望这对你有帮助!:)

查看和的文档

find_all()

find_all()

查找

find_all()
方法扫描整个文档以查找结果,但有时您只想查找一个结果。如果你知道 文档只有一个
标记,扫描文档是浪费时间 整个文档正在查找更多信息。而不是传入
limit=1
每次调用
find\u all
,都可以使用
find()
方法

因此,您不需要循环使用
find()
函数来获取标记。您需要对代码进行以下更改(删除了不必要的
for
循环):


你应该建立一些备用方案:如果没有英文字幕,或者没有最高评分的字幕怎么办。另请参阅我签出了你的链接,但问题是“dlink.find('a')['href'],应该返回电影字幕的下载链接。至于回退,一旦我以基本形式使这个脚本正常工作,我会修改它。谢谢你的回复!你试着在links.find中为dlink.find(“td”,{“class”)更改上面的这一行吗下载单元格“}):
到这个
链接中的dlink。find_all(“td”,“class”:“download cell”}):
@Abdullah Ahmed Ghaznavi谢谢!它起作用了。但是在我的代码上下文中,“find()”和“find_all()”方法有什么区别吗?欢迎!:)唯一的区别是
find_all()
返回一个包含单个结果的列表,
find()
只返回结果。在您的例子中,您是在单个元素上运行循环,而不是在列表上运行循环。感谢python文档片段!
...
# Previous code is the same

soup2 = BeautifulSoup(next_page.content,'lxml')
for links in soup2.find_all("tr",{"class": "high-rating"}):
    if links.find("td", {"class": "flag-cell"}).text == "English":
        print("After if : {}".format(links))
        half_dlink = links.find('td', {'class': 'download-cell'}).a['href']
        download = base_url + half_dlink
        print(download)