我想使用BeautifulSoup庫從網站上刮取一些定居點的名稱。該網站使用'windows-1250'字符集,但某些字符顯示不正確。查看解決方案的姓氏,這應該是Župkov。使用BeautifulSoup無法正確顯示字符
你能幫我解決這個問題嗎? 這是代碼:
# imports
import requests
from bs4 import BeautifulSoup
from bs4 import NavigableString
# create beautifulsoup object
obce_url = 'http://www.e-obce.sk/zoznam_vsetkych_obci.html?strana=2500'
source_code = requests.get(obce_url)
plain_text = source_code.text
obce_soup = BeautifulSoup(plain_text, 'html.parser')
# define bs filter
def soup_filter_1(tag):
return tag.has_attr('href') and len(tag.attrs) == 1 and isinstance(tag.next_element, NavigableString)
# print settlement names
for tag in obce_soup.find_all(soup_filter_1):
print(tag.string)
我使用Python 3.5.1和4.4.1 beautifulsoup。
謝謝你的回答。它按預期工作。 – user21816