简体   繁体   中英

Extract data using bs4 from a javascript text span

im trying to extract some data from a span that is after a text/javascript script, i tried with regex both its to fragile: how can i get the span after text/javascript?

html_content = urlopen('https://www.icewarehouse.com/Bauer_Vapor_1X/descpage-V1XS7.html')

soup = BeautifulSoup(html_content, "lxml")

price =soup.find(class_='crossout')
span = price('span')
print(span) 

output disired:

 649.99 949.99

I think you are trying to get the minimum and maximum of the array msrp . In which case you can't use BS for that. Use plain re.

Try this:

from urllib.request import urlopen
from bs4 import BeautifulSoup
import re

html_content =urlopen('https://www.icewarehouse.com/Bauer_Vapor_1X/descpage-V1XS7.html')
soup = BeautifulSoup(html_content, "lxml") 
pattern = re.compile("msrp.push\((.*?)\);.*msrp.push\((.*?)\);")
m = pattern.search(soup.text)
if m:
    print(m[1], m[2])

This uses two capturing groups to get the minimum and maximum values from the line where values are pushed into the array msrp .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM