【发布时间】:2021-05-15 16:25:27
【问题描述】:
当我尝试抓取页面上所有显卡的品牌时,它适用于前 15 个,但后来我得到了TypeError: 'NoneType' object is not subscriptable。
from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as uReq
# the url we want to scrape and saves it to a variable
url = 'https://www.newegg.com/p/pl?d=graphics+card&RandomID=551877219014822520210210001440&PageSize=36'
# opens the url and returns a file object
uClient = uReq(url)
# reads the object and returns the html contents as a string
page_html = uClient.read()
# closes the file
uClient.close()
# html parsing
page_soup = soup(page_html, "html.parser")
# grabs each element with the class of item container and stores in a variable
containers = page_soup.findAll("div", {"class": "item-container"})
# scraping the brands of each graphics card from the website
for container in containers:
brand = container.div.div.a.img["title"]
print(brand)
【问题讨论】:
标签: python web-scraping beautifulsoup