Skip to main content
 首页 » 编程设计

Python Beautiful Soup 刮多表

2025年01月19日10oomusou

我正在使用 Python 和 Beautiful Soup 来抓取 newegg 网站并获取产品定价、名称和运费。但是,当我运行该程序时,输出仅发回网站的第一个产品条目。有人可以帮我解决我做错的事吗?

# import beautiful soup 4 and use urllib to import urlopen 
import bs4 
from urllib.request import urlopen as uReq 
from bs4 import BeautifulSoup as soup 
 
# url where we will grab the product data 
my_url = 'http://www.newegg.com/Video-Cards-Video-Devices/Category/ID-38? 
Tpk=graphics%20card' 
 
# open connection and grab the URL page information, read it, then close it 
uClient = uReq(my_url) 
page_html = uClient.read() 
uClient.close() 
 
# parse html from the page 
page_soup = soup(page_html, "html.parser") 
 
# find each product within the item-container class 
containers = page_soup.findAll("div",{"class":"item-container"}) 
 
# write a file named products.csv with the data returned 
filename = "products.csv" 
f = open(filename, "w") 
 
# create headers for products 
headers = "price, product_name, shipping\n" 
 
f.write("") 
 
# define containers based on location on webpage and their DOM elements 
for container in containers: 
    price_container = container.findAll("li", {"class":"price-current"}) 
    price = price_container[0].text.strip("|") 
 
    title_container = container.findAll("a", {"class":"item-title"}) 
    product_name = title_container[0].text 
 
    shipping_container = container.findAll("li",{"class":"price-ship"}) 
    shipping = shipping_container[0].text.strip() 
 
# print each product with the brand, product name and shipping cost 
print("price: " + price) 
print("product name: " + product_name) 
print("shipping: " + shipping) 
 
# when writing each section, add a comma, replace comma with pipe, 
# add new line after shipping 
f.write(price + "," + product_name.replace(",", "|") + "," + shipping +  
"\n") 
 
f.close() 

请您参考如下方法:

打印和写入语句应该放在 for block 中。

# define containers based on location on webpage and their DOM elements for container in containers: 
For container in containers: 
    price_container = container.findAll("li", {"class":"price-current"}) 
    price = price_container[0].text.strip("|") 
 
    title_container = container.findAll("a", {"class":"item-title"}) 
    product_name = title_container[0].text 
 
    shipping_container = container.findAll("li" {"class":"price-ship"}) 
    shipping = shipping_container[0].text.strip() 
 
    # print each product with the brand, product name and  
    shipping cost 
    print("price: " + price) 
    print("product name: " + product_name) 
    print("shipping: " + shipping) 
 
    # when writing each section, add a comma, replace comma with pipe, 
    # add new line after shipping 
    f.write(price + "," + product_name.replace(",", "|") + "," + shipping +  "\n") 
 
f.close()