Python BeautifulSoup find_all'ascii'编解码器无法解码字节

时间:2015-11-07 14:13:10

标签: python beautifulsoup

我正在使用BeautifulSoup从Krasnodar krai elections中提取到不同选举分区域结果的链接。可以使用单词“option”检测来自html的链接。 html片段:

<form name="go_reg">Нижестоящие избирательные комиссии:  <select name="gs"><option val="">---</option>
<option value="http://www.krasnodar.vybory.izbirkom.ru/region/krasnodar?action=show&amp;root=123400103&amp;tvd=2232000821616&amp;vrn=2232000821581&amp;prver=2&amp;pronetvd=1&amp;region=23&amp;sub_region=23&amp;type=381&amp;vibid=2232000821616">1 Абинская</option>
<option value="http://www.krasnodar.vybory.izbirkom.ru/region/krasnodar?action=show&amp;root=123402603&amp;tvd=2232000821591&amp;vrn=2232000821581&amp;prver=2&amp;pronetvd=1&amp;region=23&amp;sub_region=23&amp;type=381&amp;vibid=2232000821591">2 Анапская</option>

这是我的代码:

import urllib2
from bs4 import BeautifulSoup
contenturl = "http://www.krasnodar.vybory.izbirkom.ru/region/region/krasnodar?action=show&root=1&tvd=2232000821586&vrn=2232000821581&region=23&global=&sub_region=23&prver=2&pronetvd=1&vibid=2232000821586&type=381"
soup = BeautifulSoup(urllib2.urlopen(contenturl).read(), 'html.parser', from_encoding = 'windows-1252')
soup.find_all('option')

但是,我收到了以下错误:

 UnicodeDecodeError:'ascii' codec can't decode byte 0xc3 in position 283: ordinal not in range(128)

我试图找到答案,其中一条建议是使用“from_encoding”选项手动设置编码,但这并没有解决问题

1 个答案:

答案 0 :(得分:0)

如果你打开美丽的汤,你可以避免错误。

from bs4 import BeautifulSoup
from urllib.request import urlopen

url = "http://www.krasnodar.vybory.izbirkom.ru/region/region/krasnodar?action=show&root=1&tvd=2232000821586&vrn=2232000821581&region=23&global=&sub_region=23&prver=2&pronetvd=1&vibid=2232000821586&type=381" #/air-jordan-1-retro-high-og-unc-white-dk-powder-blue-012304"
page=urllib.request.urlopen(url)
soup = BeautifulSoup(page.read())
for myValue in soup.find_all('option'):
    try:
        print(myValue.get_text() + " : " + myValue.get("value"))
    except:
        print("")

生成

1 Àáèíñêàÿ : http://www.krasnodar.vybory.izbirkom.ru/region/krasnodar?action=show&root=123400103&tvd=2232000821616&vrn=2232000821581&prver=2&pronetvd=1&region=23&sub_region=23&type=381&vibid=2232000821616
2 Àíàïñêàÿ : http://www.krasnodar.vybory.izbirkom.ru/region/krasnodar?action=show&root=123402603&tvd=2232000821591&vrn=2232000821581&prver=2&pronetvd=1&region=23&sub_region=23&type=381&vibid=2232000821591
3 Àïøåðîíñêàÿ : http://www.krasnodar.vybory.izbirkom.ru/region/krasnodar?action=show&root=123400203&tvd=2232000821617&vrn=2232000821581&prver=2&pronetvd=1&region=23&sub_region=23&type=381&vibid=2232000821617
....etc