如何从< script>中提取var(值)使用beautifulsoup的html的 [英] How to extract var (values) from <script> of html using beautifulsoup

查看:733
本文介绍了如何从< script>中提取var(值)使用beautifulsoup的html的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用

import requests
from bs4 import BeautifulSoup

source = requests.get('www.randomwebsite.com').text
soup = BeautifulSoup(source,'lxml')
details= soup.find('script')

这将返回以下脚本.

     <script>
var Url = "https://www.example.com";
                if(Url != ''){code}
 else {code
}
  </script>

我希望获得以下输出. https://www.example.com

i want to have the output as following. https://www.example.com

推荐答案

要打印cashback_url,您可以尝试以下脚本:

To print the cashback_url, you can try this script:

import re
import requests


url = 'https://tracking.earnkaro.com/visitretailer/508?id=103894&shareid=ENKR2020090345700421&dl=https%3A%2F%2Fwww.amazon.in%2Fgp%2Fproduct%2FB08645RXJ6%2Fref%3Dox_sc_act_title_1%3Fsmid%3DAT95IG9ONZD7S%26psc%3D1'
html_data = requests.get(url).text

cashback_url = re.search(r'var cashbackUrl = "(.*?)"', html_data).group(1)

print(cashback_url)

打印:

https://www.amazon.in/gp/product/B08645RXJ6/ref=ox_sc_act_title_1?smid=AT95IG9ONZD7S&psc=1&ck&tag=EK003221-21

这篇关于如何从&lt; script&gt;中提取var(值)使用beautifulsoup的html的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆