首页 > 解决方案 > Python:BeautifulSoup - 从 location.href= 中提取值

问题描述

我遇到了一个问题声明,我需要从代码中打印键“location.href=”的值“input.php?id=293”。

<script>location.href="input.php?id=293";</script>

我已经尝试了下面的代码,但最终卡住了。

import requests
from bs4 import BeautifulSoup
import re

url = https://localhost/sample.php
response = requests.get(url)
soup = BeautifulSoup(response.text, features="lxml")
value = soup.find_all(text=re.compile(r'location.href'))
print (value)

任何帮助将不胜感激。

标签: pythonbeautifulsoup

解决方案


可以这样解决:

from bs4 import BeautifulSoup

data = BeautifulSoup('<script>location.href="input.php?id=293";</script>')
data.get_text()[15:-2]

推荐阅读