我试图刮一个网站,但我的BeautifulSoup返回:'NoneType' object has no attribute 'get_text'
。但是,元素确实存在。
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get(url, headers=headers)
#time.sleep(60)
soup = BeautifulSoup(response.content, 'html.parser')
NumArticle = url.split('/')[-2]
titreArticle = soup.find("h1", {"class":"wi-article-title article-title-main"}).get_text()
我用的是:headers = {'User-Agent': 'Mozilla/5.0'} response = requests.get(url, headers=headers)
否则我就会得到403错误。我尝试使用time.sleep(x)
,因为我在论坛上看到它可以解决问题,但在我的情况下,它没有工作。
我认为你的问题在于你搜索两个不同类的方式。参考例句:
soup.find('h1', class_=['wi-article-title', 'article-title-main'])