使用bs4从JavaScript文本范围中提取数据 [英] Extract data using bs4 from a javascript text span
本文介绍了使用bs4从JavaScript文本范围中提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图从文本/javascript脚本之后的跨度中提取一些数据,我尝试使用正则表达式将其都变得脆弱: 在text/javascript之后如何获取跨度?
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)
输出不良:
649.99 949.99
推荐答案
我认为您正在尝试获取数组msrp
的最小值和最大值.在这种情况下,您不能使用BS.使用普通格式.
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.
尝试一下:
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])
这使用两个捕获组从将值压入数组msrp
的行中获取最小值和最大值.
This uses two capturing groups to get the minimum and maximum values from the line where values are pushed into the array msrp
.
这篇关于使用bs4从JavaScript文本范围中提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文