이 웹 사이트에서 이름과 가격을 긁어 내고 싶습니다.
이름과 가격은 모두 div
태그 내에 있습니다.
이름:
가격
이름 인쇄는 잘 작동하지만 Price 인쇄시 오류가 발생합니다.
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)
그 차이점은 무엇입니까?
그렇다면 왜 그들 중 하나는 나에게 오류를주고 다른 하나는 그렇지 않습니까?
파이썬이 해당 통화 기호에 문제가 있기 때문에 그 오류가 발생합니다. 인도 루피 기호는 언어에 따라 다르게 해석 되며 기본적으로 파이썬 charmap에 없습니다. 마지막 인쇄 명세서를로 변경 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'
이 출력은 매우 예쁘지 않고 아마도 사용할 수 없을 것이므로 인쇄하기 전에 개인적으로 해당 기호를 자릅니다. 파이썬이 정말로 인도 루피 기호를 인쇄하기를 원한다면, 당신의 charmap에 추가 할 수 있습니다. 이 게시물의 다음 단계 에 따라 charmap에 사용자 지정을 추가 하십시오 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다