我試圖從鏈接列表中抓取鏈接(全部指向同一網(wǎng)站上的不同頁面),但我一直在運行 403 錯誤。這是我試圖抓取的鏈接示例https://www.spectatornews.com/page/6/?s=bandhttps://www.spectatornews.com/page/7/?s=band等等。這是我的代碼:getarticles = []from bs4 import BeautifulSoupimport urllib.requestfor i in listoflinks: resp = urllib.request.urlopen(i) soup = BeautifulSoup(resp, from_encoding=resp.info().get_param('charset')) for link in soup.find_all('a', href=True): getarticles.append(link['href'])我一直在嘗試在 Python 3 Web Scraping 中使用HTTP 錯誤 403 中的一些答案,但我沒有取得太大的成功。我不確定我是否正確地將它們應(yīng)用于我的整個鏈接列表。我試圖通過使用標(biāo)頭來使用以下解決方案之一,但返回 HTTP 406 錯誤:不可接受這是我試圖修復(fù)的代碼:getarticles = []from bs4 import BeautifulSoupfrom bs4 import BeautifulSoupimport urllib.requestfor i in listoflinks: req=urllib.request.Request(i, headers={'User-Agent': 'Mozilla/5.0'}) resp = urllib.request.urlopen(req) soup = BeautifulSoup(resp, from_encoding=resp.info().get_param('charset')) for link in soup.find_all('a', href=True): getarticles.append(link['href'])任何幫助是極大的贊賞。我對此很陌生,因此您可以盡可能多地解釋和幫助。我只想從我的網(wǎng)站列表中收集鏈接!
從帶有 403 錯誤 python 的網(wǎng)站抓取鏈接
ibeautiful
2021-10-10 16:29:43