简体   繁体   English

Python:BeautifulSoup - 从 location.href= 中提取值

[英]Python: BeautifulSoup - extract value from location.href=

I encountered a problem statement where I need to print the value "input.php?id=293" of the key "location.href=" from the code.我遇到了一个问题陈述,我需要从代码中打印键“location.href=”的值“input.php?id=293”。

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

I have tried the below code but stuck in the end.我试过下面的代码,但最后卡住了。

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)

Any help will be appreciated.任何帮助将不胜感激。

It can be solved like this:可以这样解决:

from bs4 import BeautifulSoup

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

Try this:试试这个:

import re
from bs4 import BeautifulSoup

txt = """<script>location.href="input.php?id=293";</script>"""
soup = BeautifulSoup(txt, "html.parser")

pattern = re.compile(r'("input\.php\?id=293")')
value = soup.find_all(text=pattern)
result = pattern.search(str(value)).group()
print(result)

Output: Output:

"input.php?id=293"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM