爬虫入门学习笔记 encode, decode

爬虫入门学习笔记 encode, decode,第1张

1. Install library

install requests and BeautifulSoup libraries 
python3 -m pip install requests

python3 -m pip install BeautifulSoup4

2. Copy libraries folders from local installed path to Pycharm's env site-packages 3.Import libraries into python script

import requests

from bs4 import BeautifulSoup

url='http://xxxxx'
res=requests.get(url)
content=res.text
soup=BeautifulSoup(content,'html.parser')
all_ccy=soup.find_all(class_='Currency')

4. Get USD and Pound currency from website and display

Problem: the currency is displayed like this : £8.87

Solution: first to verify the response encoding

print(res.encoding)

-->ISO-8859-1

try to decode with utf-8

change  content=res.text  to  content=res.text.encode("ISO-8859-1").decode('utf-8')

the final output  : £8.87

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/876032.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-13
下一篇2022-05-13

发表评论

登录后才能评论

评论列表(0条)

    保存