我正在制作一个程序,这个程序可以抓取这个网站。数据是收集的,它只有项目的名称,我可以使用它们的平台,以及它们的价格。我已经为每一个抓取的信息建立了一个数据结构。但我在创建字典时会被提示输入错误?在
我使用的是python3.7.2。在windows 10上运行。在
import requests
import bs4
import time
from bs4 import BeautifulSoup as Bsoup
url = "https://ebgames.com.au/search?q=Skyrim"
resp = requests.get(url)
soup = Bsoup(resp.text, 'html.parser')
platforms = soup.select(".product-top-level-group")
price = soup.select(".price")
names = soup.select(".product-title")
stripped_names = [na.text.strip() for na in names]
stripped_prices = [pri.text.strip() for pri in price]
stripped_platforms = [plat.text.strip() for plat in platforms]
Game = {
(stripped_names): {
"Price": (stripped_prices),
"Platform": [stripped_platforms]
}
}
for Gamename, Gameinfo in Game.items():
print(Gamename)
print("Platform:", Gameinfo['Platform'])
print("Price:", Gameinfo['Price'])
print("\n")
这是我的错误:
^{pr2}$
不确定您从哪里得到的
dict
初始化语法,但在Python中不是这样做的。在下面是一个使用
zip
完成此操作的好方法:输出:
^{pr2}$问题是您试图用一个不可散列的键初始化dict,这里是
stripped_names
(您可以阅读this来了解有关散列对象的更多信息)。您可以将其转换为tuple
以使其可散列,或者选择另一个键。在下一次,别忘了把你的错误全部回溯出来,它会帮助人们更快更好地帮助你。在
相关问题 更多 >
编程相关推荐