Extract data using bs4 from a javascript text span

末鹿安然 提交于 2020-06-23 18:41:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!