我要從亞馬遜上抓取一些頁面。我想存盤一些產品的標題。但是我的編碼有問題。
def get_information_products(href):
url = 'https://www.amazon.fr' href
url = Request(url)
ua = UserAgent()
url.add_header('User-Agent', ua.random)
with urlopen(url) as f:
data = f.readlines()
page_soup = soup(str(data), 'html.parser', from_encoding='iso-8859-1')
title_list = []
try:
title = page_soup.find("span", attrs={"id": 'productTitle'})
print(title.get_text(strip=True))
return title.get_text(strip=True)
except:
return ''
這是獲取資料的一段代碼。之后,我要將資料保存到 csv。但我總是有同樣的問題。我的產品標題是這樣的:
OVO Sthira - Lot de 2 Briques de Yoga en Li\xc3\xa8ge Premium - Ultra Fin - Bloc Yoga - Brique Yoga - Block Yoga - Accessoire de Yoga \xc3\xa9cologique
我不知道如何用正確的字符保存資料......
uj5u.com熱心網友回復:
看來你的頁面標題是 UTF8 的,你可以試試這個:
str = title.get_text(strip=True)
str.encode("windows-1252").decode('utf8')
如果它是一個純字串,您可能需要一個額外的步驟:
str.decode("utf-8").encode("windows-1252").decode("utf-8")
uj5u.com熱心網友回復:
您可以嘗試使用unicodedata模塊
import unicodedata
unicodedata.normalize("NFKD",your_text)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/417669.html
標籤:
