[英]Python beautifulSoup webpage decode problem
我正在尝试使用 beautifulSoap4抓取以下两页
两者具有相同的 HTML 结构。 当我加载第一个网页时,一切都很好,我得到了这个:
<!DOCTYPE html>
<html dir="rtl" lang="fa-IR">
<head>
<style id="litespeed-optm-css-rules">
...
但是第二个网页output是这样的:
Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.
�[s�Ƶ(��_���!��3�+ E:�|���lmI����.UИ��& ���!���p���'ە�����~��?1��̩� f0�\ q�
<u*q�"�f��v�[�^}��~|�����e����4� 94�,4�pf�cӗ��̣[="%��[iv*#��0�T:P�kŃ��rӴ�" c��gm_vv۾l�gz���_���yˏ�����8�qw��ȳԕ�:h����="" �@��;��tʳ�="" �h�:a�="" ��@fy="">
=���
这是我的 python 代码:
from urllib.request import Request, urlopen
from bs4 import BeautifulSoup as soup
url = 'https://30nama.kim/top/30nama-movie.html'
req = Request(url , headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
page_soup = soup(webpage, "html.parser")
print(page_soup.prettify())
我不知道第二页发生了什么,这些字符是什么意思。 我想我应该尝试使用 utf-8 对其进行解码,但它没有用。 有任何想法吗?
BeautifulSoup 使用 Unicode,该死的检测编码。 这并不总是正确的。
我手动输入编码并且它起作用了:
page_soup = soup(webpage, "html.parser", from_encoding="ISO-8859-7")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.