Python 在bs4中为易趣物品创建for循环
我有以下代码,可以进入ebay url并提取物品名称和销售价格,请参见以下内容:Python 在bs4中为易趣物品创建for循环,python,loops,beautifulsoup,iterator,Python,Loops,Beautifulsoup,Iterator,我有以下代码,可以进入ebay url并提取物品名称和销售价格,请参见以下内容: from urllib.request import urlopen from bs4 import BeautifulSoup import request html = urlopen('https://www.ebay.com/sch/i.html?_from=R40&_nkw=manga&_sacat=0&rt=nc&LH_Sold=1&LH_Complete=1'
from urllib.request import urlopen
from bs4 import BeautifulSoup
import request
html = urlopen('https://www.ebay.com/sch/i.html?_from=R40&_nkw=manga&_sacat=0&rt=nc&LH_Sold=1&LH_Complete=1')
soup = BeautifulSoup(html.read(), 'html.parser')
soldItem = soup.find('h3', class_='s-item__title s-item__title--has-tags')
salePrice = soup.find('span', class_='POSITIVE')
#data = soup.find('div', class_='s-item__info clearfix')
itemData = {soldItem.get_text():salePrice.get_text()}
我想创建一个for循环,循环遍历第一页,并给出每个列表的名称和销售价格
然而,我所做的每一次尝试都会返回五次相同的列表,或者返回所有已售出的商品,然后返回所有的销售价格
关于如何格式化for循环的任何提示?您可以尝试以下代码,该代码将通过项目名称键和价格值在字典中创建所有项目及其价格
from urllib.request import urlopen
from bs4 import BeautifulSoup
import requests
html = urlopen('https://www.ebay.com/sch/i.html?_from=R40&_nkw=manga&_sacat=0&rt=nc&LH_Sold=1&LH_Complete=1')
soup = BeautifulSoup(html.read(), 'html.parser')
soldItem = soup.find_all('h3', class_='s-item__title s-item__title--has-tags')
salePrice = soup.find_all('span', class_='POSITIVE')
itemsData = {item.text: price.text for item, price in zip(soldItem, salePrice)}
print(itemsData)
这是解决我的问题的一种方法,我仍然有创建for循环的问题。也就是说,你能检查一下名为itemsData,idk的变量吗?为什么这样做?我不确定需要什么,你能提供更多的细节吗?谢谢你的帮助!不用担心,我找到了这个问题的答案。我必须更详细地研究你的答复,看看它为什么有效。谢谢