我想从这个网站上搜刮一下名字和价格:
名称和价格都在div
标记中。你知道吗
姓名:
价格
打印名称很好,但打印价格给了我一个错误:
Traceback (most recent call last):
File "c:\File.py", line 37, in <module>
print(price.text)
File "C:\Python37\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u20b9' in position 0: character maps to <undefined>
代码:
from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
import requests
response = requests.get("https://www.flipkart.com/laptops/~buyback-guarantee-on-laptops-/pr?sid=6bo%2Cb5g&uniq")
soup = BeautifulSoup(response.text, 'html.parser')
for a in soup.findAll('a',href=True, attrs={'class':'_31qSD5'}):
name=a.find('div', attrs={'class':'_3wU53n'})
price=a.find('div', attrs={'class':'_1vC4OE _2rQ-NK'})
print(name.text)
它们之间有什么区别?你知道吗
为什么其中一个给了我一个错误而另一个没有?你知道吗
它产生了这个错误,因为python在货币符号方面遇到了问题。印度卢比符号的解释不同depending on the language,默认情况下不在python charmap中。如果我们将上一个print语句更改为
print(str(price.text.encode("utf-8")))
,我们将得到如下结果:b'\xe2\x82\xb961,990' b'\xe2\x82\xb940,000' b'\xe2\x82\xb963,854' b'\xe2\x82\xb934,990' b'\xe2\x82\xb948,990' b'\xe2\x82\xb952,990' b'\xe2\x82\xb932,990' b'\xe2\x82\xb954,990' b'\xe2\x82\xb952,990'
因为这个输出不是很漂亮,而且可能不可用,所以我个人会在打印之前截断这个符号。如果您真的希望python打印印度卢比符号,可以将其添加到charmap中。按照this post中的步骤向charmap添加定制。你知道吗
相关问题 更多 >
编程相关推荐