我一直在嘗試使用 lxml lib 從一些網站獲取數據。和Python3。但在網絡抓取過程之后,我得到了一些奇怪的字符而不是土耳其字符。奇怪的字符如下所示。土耳其殘疾人運動援助和教育總局 (TESYEV)關于單科考試的公告2019-2020 伊利學院研究院但它們應該像下面給出的那樣。土耳其殘疾人運動援助和教育基金會 (TESYEV) 總局關于單科考試的公告我們的學生在 2019-2020 學年要做的程序我從不同的網站得到了每個句子。我不知道如何將它們轉換為土耳其語文本。這是我的代碼。import cssselectimport requestsfrom lxml import htmldef parse_html(url, selector): page = requests.get(url) tree = html.fromstring(page.content) titles = tree.cssselect(selector) for title in titles: print(title.text_content().strip())版本蟒蛇= 3.7.4lxml = 4.5.2請求= 2.24.0css選擇= 1.1.0
1 回答

RISEBY
TA貢獻1856條經驗 獲得超5個贊
回答
import cssselect
import requests
from lxml import html
def parse_html(url, selector):
? ? page = requests.get(url)
? ? content = str(page.content, 'utf-8')
? ? tree = html.fromstring(content)
? ? titles = tree.cssselect(selector)
? ? for title in titles:
? ? ? ? print(title.text_content().strip())
為什么
unicode 字符“?”(U+0131)在 UTF-8 中編碼為0xC4B1 。2 字節。
> echo -e '\u0131' | xxd -u
00000000: C4B1 0A? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ...
page.content
返回二進制響應內容。
0xC4B1變為0xC4?(U+00C4 '?') 和0xB1?(U+00B1 '±')
并且U+00FC 'ü'(UTF-8 編碼:0xC3BC)變為0xC3?(U+00C3 'à') 和0xBC?(U+00BC '?')
添加回答
舉報
0/150
提交
取消