当我通过Chrome DevTools检查代码时,文字没问题但是一旦被删除我就会出现字符错误。
e.g。在下面的代码中,h1应该返回“Valerian e la citt à dei mille pianet”而不是“Valerian e la citt à dei mille pianeti”。
在抓取此域上的任何文本时,这些字符错误会重复出现。
我不明白为什么,就像在其他网站中这个代码完美无缺。
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import requests
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get('http://www.mymovies.it/film/2017/valerian/', headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
title = soup.find('h1').get_text()
print(title)
答案 0 :(得分:1)
解决!
我检查了@unutbu链接,我强制将请求编码为utf-8,即使在标题中定义了它。
response.encoding = 'utf-8'