简体   繁体   English

Python Beautiful Soup没有循环结果

[英]Python Beautiful Soup not looping results

Im using BS4 for the first time and need to scrape the items from an online catalogue to csv. 我是第一次使用BS4,需要从在线目录中将项目抓取到csv。 I have setup my code however when i run the code the results are only repeating the first item in the catalogue n times (where n is the number of items). 我已经设置了代码,但是当我运行代码时,结果仅重复目录中的第一个项目n次(其中n是项目数)。 Can someone review my code and let me know where i am going wrong. 有人可以查看我的代码并让我知道我要去哪里了。

Thanks 谢谢

import requests
from bs4 import BeautifulSoup
from csv import writer


#response = requests.get('https://my.supplychain.nhs.uk/Catalogue/browse/27/anaesthetic-oxygen-and-resuscitation?CoreListRequest=BrowseCoreList')
response = requests.get('https://my.supplychain.nhs.uk/Catalogue/browse/32/nhs-cat?LastCartId=&LastFavouriteId=&CoreListRequest=BrowseAll')

soup = BeautifulSoup(response.text , 'html.parser')
items = soup.find_all(class_='productPrevDetails')
#print(items)
for item in items:
    ItemCode = soup.find(class_='product_npc ').get_text().replace('\n','')
    ItemNameS = soup.select('p')[58].get_text()    
    ProductInfo = soup.find(class_='product_key_info').get_text()

    print(ItemCode,ItemNameS,ProductInfo)

You always see the first result because you are searching soup , not the item . 您总是看到第一个结果,因为您正在搜索soup ,而不是item Try 尝试

for item in items:
    ItemCode = item.find(class_='product_npc ').get_text().replace('\n','')
    ItemNameS = item.select('p')[58].get_text()    
    ProductInfo = item.find(class_='product_key_info').get_text()

    print(ItemCode,ItemNameS,ProductInfo)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM