pycharm 使用BeautifulSoup的Webscraping AttributeError:“NoneType”对象没有属性“find”

mgdq6dx1  于 2023-06-29  发布在  PyCharm
关注(0)|答案(1)|浏览(198)

我需要你的帮助我在代码中哪里出错了?我正在尝试用BeautifulSoup创建一个网站:

你能帮我解决这个问题吗?
我检查了我的代码,试图解决这个问题,但我找不到解决方案。

vhipe2zx

vhipe2zx1#

使用try except或check响应状态代码

from bs4 import BeautifulSoup as bs
import requests

def main():
    url = 'https://subslikescript.com/movies'
    root = 'https://subslikescript.com'
    response = requests.get(url)
    if response.status_code == 200:
        soup = bs(response.content, 'lxml')
        box = soup.find('article', class_='main-article')
        links = []
        for link in box.find_all('a',href=True):
            links.append(link['href'])

        #print(links)
        for link in links:
            response = requests.get(f"{root}/{link}")
            if response.status_code == 200:
                soup = bs(response.content, 'lxml')
                box = soup.find('article', class_='main-article')
                title = box.find('h1')
                if title:
                    print(title)
            else:
                print(link, response.status_code)
    else:
        print(response.status_code)

if __name__ == '__main__':
    main()

相关问题