AttributeError:“ NoneType”对象在网络抓取期间没有属性“ get_text”

时间:2020-06-08 11:03:19

标签: python web-scraping

我试图创建一些代码来搜索亚马逊的产品并显示价格。代码就在这里:

import requests
import pyshorteners
import smtplib
from bs4 import BeautifulSoup
from googlesearch import search

headers = {
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0"
}


def sughetto2():
    sito = requests.get(i, headers=headers)
    sughetto = BeautifulSoup(sito.content, 'html.parser')
    nome = sughetto.find(id="productTitle")
    nome = nome.get_text()
    prezzo = sughetto.find(id="priceblock_ourprice")
    prezzo = prezzo.get_text(strip=True)
    prezzo = prezzo.replace(",", ".")
    prezzo = float(prezzo[0:6])
    print(prezzo)

nomeProdotto = input("Inserisci il nome del prodotto che vuoi cercare: \n")
nomeProdotto = nomeProdotto +str(" amazon")
for i in search(query=nomeProdotto, tld="com", lang="it", tbs=0, safe="off", num=5, start=0, stop=5, domains=None, pause=2.0, tpe='', country='', extra_params=None, user_agent=None):
    print(i)
    try:
        sughetto2()
    except AttributeError:
        sughetto2()

我想遍历几个URL,在第一个URL实际起作用之后,在第二个URL期间出现以下错误:

"Traceback (most recent call last):
  File "/home/benny/Scrivania/Coding/amazonPrezzi.py", line 34, in <module>
    sughetto2()
  File "/home/benny/Scrivania/Coding/amazonPrezzi.py", line 22, in sughetto2
    nome = nome.get_text()
AttributeError: 'NoneType' object has no attribute 'get_text'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/benny/Scrivania/Coding/amazonPrezzi.py", line 36, in <module>
    sughetto2()
  File "/home/benny/Scrivania/Coding/amazonPrezzi.py", line 22, in sughetto2
    nome = nome.get_text()
AttributeError: 'NoneType' object has no attribute 'get_text'".

我尝试了一下谷歌搜索,但没有找到任何解决方案。你们愿意帮助我吗?已经感谢您阅读此内容:')

0 个答案:

没有答案