Ganci Sun
Ganci Sun

Reputation: 21

How may I scrape the price under the li

Here are my codes:

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"})

The problem came with these two lines below:

pricetag = container.findAll("li",{"class":"price-current"})
pricetag

The results are: enter image description here All I want is to get the price, which is

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

How may I do it?

Upvotes: 0

Views: 53

Answers (1)

The fourth bird
The fourth bird

Reputation: 163187

To get the price you might use a css selectordiv.item-container li.price-current strong to get the strong elements and use findNextSibling to get the sup element.

containers = page_soup.select("div.item-container li.price-current strong")

for c in containers:
    print(c.text +  c.findNextSibling('sup').text)

That will result in:

599.99
369.99
..

Upvotes: 1

Related Questions