问题
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
回答1:
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.
来源:https://stackoverflow.com/questions/61088459/extract-data-using-bs4-from-a-javascript-text-span