TypeError:“ NoneType”对象不可下标,BeautifulSoup Webscraping

时间:2018-11-18 14:17:26

标签: python web-scraping beautifulsoup

请帮助。我在下面出现TypeError:

Traceback (most recent call last):
  File "my_first_webscrape.py", line 22, in <module>
    brand_name = brand[0].img["title"]
TypeError: 'NoneType' object is not subscriptable

这是我的代码:

import bs4

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

my_url = 'https://www.newegg.com/global/ph-en/Laptops-Notebooks/SubCategory/ID-32?Tid=702226'

uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()

page_soup = soup(page_html, "html.parser")

containers = page_soup.findAll("div",{"class":"item-container"})
container = containers[0]

for container in containers:
    brand = container.findAll("div", {"class":"item-branding"})
    brand_name = brand[0].img["title"]

    title = container.findAll("a", {"class":"item-title"})
    title_name = title[0].text

    price = container.findAll("li", {"class":"price-current"})
    price_current = price[0].strong.text

    print("brand_name: " + brand_name)
    print("title_name: " + title_name)
    print("price_current: " + price_current)

================================================ =

我认为错误是由于brand_name返回None引起的,但我不知道如何解决。

1 个答案:

答案 0 :(得分:0)

您的代码可以正常工作,但是newegg具有反机器人功能,可能是您的IP被阻止,更改了IP或使用了代理。

尝试打印page_html,您会看到

  

机器人?

     

那不是你,对吧?