Python 在浏览阿拉伯语网站时,从阿拉伯语字母表中获取奇怪的字母
我想刮这个网站: 但当我这样做的时候,我会看到像这样的角色 这是我的脚本的外观:Python 在浏览阿拉伯语网站时,从阿拉伯语字母表中获取奇怪的字母,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我想刮这个网站: 但当我这样做的时候,我会看到像这样的角色 这是我的脚本的外观: import requests from bs4 import BeautifulSoup BASE_URL = "http://waqfeya.com/book.php?bid=1" source = requests.get(BASE_URL) soup = BeautifulSoup(source.text, 'lxml') print(soup) 我试过这些方法,但不适合我: so
import requests
from bs4 import BeautifulSoup
BASE_URL = "http://waqfeya.com/book.php?bid=1"
source = requests.get(BASE_URL)
soup = BeautifulSoup(source.text, 'lxml')
print(soup)
我试过这些方法,但不适合我:
source.encoding='utf-8'
这是:
source.encoding='ISO-8859-1'
还包括:
soup=BeautifulSoup(source.text,from_endocing='ISO-8859-1')
但是没有一个对我有效。使用
urlopen
而不是request
from bs4 import BeautifulSoup
from urllib import urlopen
BASE_URL = "http://waqfeya.com/book.php?bid=1"
open = urlopen(BASE_URL)
soup = BeautifulSoup(open, 'lxml')
print(soup.encode('utf-8'))
有时请求可能会得到错误的编码。对于这个站点,我们可以从源代码中获得正确的编码 在BeautifulSoup中使用
source.text
之前,可以像source.encoding='windows-1256'
那样指定编码
import requests
BASE_URL = "http://waqfeya.com/book.php?bid=1"
source = requests.get(BASE_URL)
print(source.encoding)
print(source.apparent_encoding)
source.encoding='windows-1256'
print(source.text)
我能够正确地获取所有阿拉伯字符。请查看此处