我的程序是一个基本的网页抓取工具,它将针对给定的卡输入返回价格。但是,我想将从刮板收到的HTML结果转换为float以便对其执行数学运算。目前,我的代码是:
import requests
from bs4 import BeautifulSoup
number = int(input("Enter the amount of cards: "))
card_list = {}
for i in range(number):
card_names = input("Enter the card name: ")
set_names = input("Enter the respective card set: ")
card_list[card_names] = set_names
for card_name, set_name in card_list.items():
url = "https://www.mtggoldfish.com/price/"+set_name+"/"+card_name+"#paper"
page = requests.get(url)
page_content = page.content
soup = BeautifulSoup(page_content, 'html.parser')
table = soup.find_all("div", {'class':"price-box-container"})
for t in table:
paper = t.find_all("div",{"class":"price-box paper"})
prices = t.findChildren("div",{"class":"price-box paper"})
for price in prices:
print(price.get_text())
通常,它返回类似PAPER 69.78
的内容,但是尝试使用prices = prices[1:]
剪切“ PAPER”不会返回任何结果。此外,尝试float:prices
返回:
TypeError: float() argument must be a string or a number, not 'ResultSet'
任何帮助将不胜感激!
答案 0 :(得分:1)
import requests
from bs4 import BeautifulSoup
number = int(input("Enter the amount of cards: "))
card_list = {}
for i in range(number):
card_names = input("Enter the card name: ")
set_names = input("Enter the respective card set: ")
card_list[card_names] = set_names
for card_name, set_name in card_list.items():
url = "https://www.mtggoldfish.com/price/"+set_name+"/"+card_name+"#paper"
page = requests.get(url)
page_content = page.content
soup = BeautifulSoup(page_content, 'html.parser')
table = soup.find_all("div", {'class':"price-box-container"})
for t in table:
paper = t.find_all("div",{"class":"price-box paper"})
prices = t.findChildren("div",{"class":"price-box paper"})
price_box_type = prices[0].find("div", class_="price-box-type").get_text(strip=True)
price_box_price = prices[0].find("div", class_="price-box-price").get_text(strip=True)
print(f"price-box-type - {price_box_type}")
print(f"price-box-price - {price_box_price}")
输出:
Enter the amount of cards: 1
Enter the card name: Fiery+Emancipation
Enter the respective card set: Core+Set+2021
price-box-type - PAPER
price-box-price - 9.87
Enter the amount of cards: 1
Enter the card name: Ereboss+Intervention
Enter the respective card set: Theros+Beyond+Death
price-box-type - PAPER
price-box-price - 0.50
答案 1 :(得分:1)
import requests
from bs4 import BeautifulSoup
number = int(input("Enter the amount of cards: "))
card_list = {}
for i in range(number):
card_names = input("Enter the card name: ")
set_names = input("Enter the respective card set: ")
card_list[card_names] = set_names
for card_name, set_name in card_list.items():
url = "https://www.mtggoldfish.com/price/"+set_name+"/"+card_name+"#paper"
page = requests.get(url)
page_content = page.content
soup = BeautifulSoup(page_content, 'html.parser')
table = soup.find_all("div", {'class':"price-box-container"})
for t in table:
paper = t.find_all("div",{"class":"price-box paper"})
prices = t.findChildren("div",{"class":"price-box paper"})
price_box_type = prices[0].find("div", class_="price-box-
type").get_text(strip=True)
price_box_price = float(prices[0].find("div", class_="price-box-
price").get_text(strip=True))
print(f"price-box-price - {price_box_price}")
print(type(price_box_price)) #just for reference
输出:
Enter the amount of cards: 1
Enter the card name: Baneslayer+Angel
Enter the respective card set: Core+Set+2021
price-box-price - 2.92
<class 'float'>