有我的代碼
def parser():
flag = True
url = 'https://quotes.toscrape.com'
while flag:
responce = requests.get(url)
soup = BeautifulSoup(responce.text, 'html.parser')
quote_l = soup.find_all('span', {'class': 'text'})
q_count = 0
for i in range(len(quote_l)):
if q_count >= 5:
flag = False
break
quote = soup.find_all('span', {'class': 'text'})[i]
if not Quote.objects.filter(quote=quote.string).exists():
author = soup.find_all('small', {'class': 'author'})[i]
if not Author.objects.filter(name=author.string).exists():
a = Author.objects.create(name=author.string)
Quote.objects.create(quote=quote.string, author_id=a.id)
q_count = 1
else:
a = Author.objects.get(name=author.string)
Quote.objects.create(quote=quote.string, author_id=a.id)
q_count = 1
url = soup.find('li', {'class': 'next'}).a['href']
我需要獲取下一頁,但我有這個 Exc。“NoneType”物件沒有屬性“a”
如何解決這個問題,也許我可以如何優化我的 Code.Thx
uj5u.com熱心網友回復:
到達最后一頁后,將沒有 Next 按鈕,因此您需要在嘗試訪問下一頁的 href 之前檢查退出條件。一種可能是在當前最后一行之前添加以下行:
next_page = soup.find('li', {'class': 'next'})
if not next_page: flag = False # or return
或者只是return在那個時候。
當然,您還需要更新最后一行以使用該變數,并確保您不會連續擴展帶有下一頁后綴的 url。例如,可以在請求呼叫期間添加后綴:
def parser():
flag = True
url = 'https://quotes.toscrape.com'
suffix = ''
while flag:
responce = requests.get(url suffix)
soup = BeautifulSoup(responce.text, 'html.parser')
# other code
next_page = soup.find('li', {'class': 'next'})
if not next_page:
return
suffix = next_page.a['href']
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/536682.html
上一篇:JPARepository中的DateGreaterThanEqual和DateIsGreaterThanEqual有什么區別?
下一篇:CultureInfo.CurrentCulture和Thread.CurrentThread.CurrentCulture之間的區別
