BeautifulSoup: find_all()在使用for循环时返回一个结果



我正在用Beautiful Soup练习网页抓取。但结果只有一个。我查了soup.find_all。当我打印出"items"时,它显示了整个列表。然而,当我添加for循环函数时,它只显示一个结果。请帮帮我,谢谢。

from bs4 import BeautifulSoup
import requests
html_text = requests.get('https://www.flipkart.com/laptops/~buyback-guarantee-on-laptops-/pr?sid=6bo%2Cb5g&uniqBStoreParam1=val1&wid=11.productCard.PMU_V2').text
soup = BeautifulSoup(html_text, 'lxml')
items = soup.find_all('div', class_= '_13oc-S')
for start_scraping in items:
product_name = start_scraping.find('div', class_ = '_4rR01T').text
product_price = start_scraping.find('div', class_ = '_30jeq3 _1_WHN1').text
product_rating = start_scraping.find('div', class_ ='_3LWZlK').text
product_description = start_scraping.find('li', class_ ='rgWa7D').text
print(f'Product Name: {product_name} nProduct Price: {product_price} nProduct Rating: {product_rating} nProduct Descrption: {product_description}')

你应该在循环中添加'print',而不是在循环之外更好的方法是pandas:

main_list=[]
for start_scraping in items:
product_name = start_scraping.find('div', class_ = '_4rR01T').text
product_price = start_scraping.find('div', class_ = '_30jeq3 _1_WHN1').text
product_rating = start_scraping.find('div', class_ ='_3LWZlK').text
product_description = start_scraping.find('li', class_ ='rgWa7D').text
main_list.extend([product_name ,product_price ,product_rating ,product_description ])
df=pandas.DataFrame(main_list)
print(df)

相关内容

  • 没有找到相关文章

最新更新