我怎么可以废除李的价格

时间:2018-06-15 14:31:52

标签: python python-3.x beautifulsoup

以下是我的代码:

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

结果如下: enter image description here 我想要的就是获得价格,这是

</span>$<strong>599</strong><sup>.99</sup>

我该怎么办?

1 个答案:

答案 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
..