【发布时间】:2021-10-31 21:41:14
【问题描述】:
我有一个 HTML 文件,该文件包含几个脚本,特别是在最后一个脚本中包含一个我想要获取的值
我需要得到这里找到的哈希值
extend(cur, { "hash": "13334a0e457f0793ec", "loginHost": "login", "sureBoxText": false, "strongCode": 0, "joinParams": false, "validationType": 3, "resendDelay": 120, "calledPhoneLen": 4, "calledPhoneExcludeCountries": [1, 49, 200] });
为此我使用了
import re
with open("test.html", "r", encoding='utf-8') as f:
html = f.read()
hash = re.search(r'{ "hash": "(.*?)",', html).group(1)
完美运行,但是当我尝试直接从请求中执行相同操作时,出现错误。
with requests.get(url, headers=headers, cookies=cookies) as response:
if response.status_code == 200:
html = response.content
hash = re.search(r'{ "hash": "(.*?)",', html).group(1)
return hash
错误
TypeError: cannot use a string pattern on a bytes-like object
然后我执行了一个简单的测试,将“response.text”保存在一个 html 文件中并尝试以第一种方式读取,但错误仍然存在 在我输入文件并在我的 vscode 中单击以格式化文件后不久,它修复了整个 html 文件,我执行了测试并且它有效。 我需要一种将“response.text”格式化为 html 的方法,这样我就可以获得我的价值,或者如果有另一种我不知道我愿意学习的方法。
OBS 哈希值在'response.text'中找到
【问题讨论】:
标签: python beautifulsoup python-requests python-re