我正在尝试为每个列出的项目选择销售价格,但这是我能得到的最接近的商品。
import requests
from bs4 import BeautifulSoup
url = 'http://www.ebay.co.uk/sch/i.html?_from=R40&_sacat=0&_nkw=graphics%20card&LH_Complete=1&LH_Sold=1&rt=nc&_trksid=p2045573.m1684'
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
Sale_Price = [tag['class'] for tag in soup.find_all("span", class_="bold bidsold")]
print(Sale_Price)
给了我: [['bold','bidold'],['bold','bidold'],['bold','bidold'],['bold', 'bidold'],['bold','bidold'],['bold','bidold'],['bold','bidold'],['bold','bidold'],['bold', 'bidold'],['bold','bidold'],['bold','bidold'],['bold','bidold'],['bold','bidold'],['bold', 'bidold'],['bold','bidold'],['bold','bidold'],['bold','bidold'],['bold','bidold'],['bold', 'bidold'],['bold','bidold'],['bold','bidold'],['bold','bidold'],['bold','bidold'],['bold', 'bidold'],['bold','bidold'],['bold','bidold'],['bold','bidold'],['bold','bidold'],['bold', 'bidold'],['bold','bidold'],['bold','bidold'],['bold','bidold'],['bold','bidold'],['bold', 'bidold'],['bold','bidold'],['bold','bidold'],['bold','bidold'],['bold','bidold'],['bold', 'bidold'],['bold','bidold'],['bold','bidold'],['bold','bidold'],['bold','bidold'],['bold', 'bidold'],['bold','bidold'],['bold','bidold'],['bold','bidold'],['bold','bidold'],['bold', 'bidold'],['bold','bidold']]
答案 0 :(得分:2)
您正在存储class
的名称。价格在string
。使用get_text()
获取string
。字符串包含很多空格或新行,使用strip()
来摆脱这些。
import requests
from bs4 import BeautifulSoup
url = 'http://www.ebay.co.uk/sch/i.html?_from=R40&_sacat=0&_nkw=graphics%20card&LH_Complete=1&LH_Sold=1&rt=nc&_trksid=p2045573.m1684'
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
Sale_Price =[ tag.get_text().strip() for tag in soup.find_all("span", class_="bold bidsold") ]
print(Sale_Price)
它给出了输出:
['£159.99', '£240.00', '£8.00', '£100.00', '£54.99', '£324.99', '£10.00', '£130.00', '£21.00', '£68.00', '£25.00', '£90.00', '£210.00', '£269.49', '£90.56', '£5.90', '£56.00', '£89.99', '£142.00', '£104.00', '£35.00', '£8.80', '£27.00', '£45.00', '£45.00', '£115.11', '£293.19', '£172.00', '£42.00', '£14.39', '£120.00', '£24.99', '£11.73', '£10.50', '£88.00', '£340.00', '£136.82', '£5.00', '£21.32', '£66.46', '£49.99', '£25.00', '£30.00', '£385.00', '£258.00', '£64.30', '£87.00', '£29.99', '£77.99', '£36.88', '£71.00']
编辑
如果你想忽略£
符号,那么拿一个没有第一个字符的字符串。
Sale_Price =[ tag.get_text().strip()[1:] for tag in soup.find_all("span", class_="bold bidsold") ]
print(Sale_Price)
这将仅存储没有£
符号的价格。