我从BeautifulSoup中获得了一个str,其中包含使用\xXX
表示法的转义字符,需要将其解码为常规str。
示例:
next_url = r'\x26hl\x3den'
转换后,我想要
next_url = '&hl=en'
一开始看起来很简单,但是经过一个小时的搜索,我仍然找不到解决方案。有什么好方法吗?
编辑:添加一些代码以响应注释。真的很简单。
session = requests.Session()
r = session.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
next_url = soup.find(class_='XXXX')['onclick'].split('=', 1)[1][1:-1] # handles: onclick="window.location='http:domain.com/path'"
next_url
需要解码。
答案 0 :(得分:2)
您有一个带字节文字的str。将编解码器模块与unicode-escape
编解码器一起使用以取消转义。
import codecs
codecs.decode(r'\x26hl\x3den', 'unicode-escape')