以下是我的代码:
import pandas as pd
import numpy as np
from urllib.request import urlopen as ureq
from bs4 import BeautifulSoup as soup
my_url = 'https://www.newegg.com/Gaming-Video-Cards/PromotionStore/ID-1197?
cm_sp=Cat_Video-Cards_1-_-TopNav-_-Gaming-Video-Cards'
my_url
gamestore = ureq(my_url)
page_html = gamestore.read()
gamestore.close()
page_soup = soup(page_html, "html.parser")
containers = page_soup.findAll("div",{"class":"item-container"})
问题来自以下两行:
pricetag = container.findAll("li",{"class":"price-current"})
pricetag
</span>$<strong>599</strong><sup>.99</sup>
我该怎么办?
答案 0 :(得分:1)
要获得价格,您可以使用css selector div.item-container li.price-current strong
获取强元素,并使用findNextSibling
获取sup
元素。
containers = page_soup.select("div.item-container li.price-current strong")
for c in containers:
print(c.text + c.findNextSibling('sup').text)
这将导致:
599.99
369.99
..