Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 间歇抛出ulsou编码错误_Python_Encoding_Beautifulsoup - Fatal编程技术网

Python 间歇抛出ulsou编码错误

Python 间歇抛出ulsou编码错误,python,encoding,beautifulsoup,Python,Encoding,Beautifulsoup,我使用BeautifulSoup解析html页面,有时它会抛出编码错误,如“编码错误:由于输入错误导致输入转换失败,字节0x9D 0x3C 0x2F 0x74” 我试图用try-except-block捕捉这个错误,以确切地知道是什么行导致了这个错误,因为我有许多行使用了BeautifulSoup,但它不起作用,因为它看起来像是BeautifulSoup只将错误报告给stderr,忽略这些字符并继续工作 r = requests.get("someurl") try: soup =

我使用BeautifulSoup解析html页面,有时它会抛出编码错误,如“编码错误:由于输入错误导致输入转换失败,字节0x9D 0x3C 0x2F 0x74” 我试图用try-except-block捕捉这个错误,以确切地知道是什么行导致了这个错误,因为我有许多行使用了BeautifulSoup,但它不起作用,因为它看起来像是BeautifulSoup只将错误报告给stderr,忽略这些字符并继续工作

r  = requests.get("someurl")
try:
     soup = BeautifulSoup(r.content,"lxml")
except Exception as e:
     print(e)

有没有办法知道哪一行确实抛出了错误,以及导致编码错误的字符是什么?

尝试
导入日志记录
,然后在
中,除了
日志记录。异常(str(e))
它不会转到异常块,因此没有eshow all Traceback。您可以尝试将
r.content
保存到字节字符串(或将其写入文件),并在其中搜索字符
'\x9d'
。但是检查该位置周围的字符,您应该能够在文本中找到问题发生的位置。我想捕获错误BeautifulSoup报告,问题是我认为BeautifulSoup只向stderr报告错误,所以它实际上不断言错误