|
|
爽快的四季豆 · XPath 3 Training | Altova· 2 月前 · |
|
|
坚强的四季豆 · C++ 上用 ONNXruntime ...· 2 月前 · |
|
|
欢乐的消防车 · Gentoo Forums :: View ...· 4 月前 · |
|
|
淡定的鼠标垫 · 引人入勝 [正文] - 成語檢視 - ...· 7 月前 · |
|
|
曾深爱过的消炎药 · .net core日志NLog的使用 - ...· 1 年前 · |
我一直在修改Python,在我的iPad上使用Pythonista。我决定写一个简单的脚本,用日语从一个网站中提取歌词,并向另一个网站发出帖子请求,该网站基本上用额外的信息对歌词进行注释。
当我在第二个网站中使用Python2和模块
mechanize
时,一切都很好,但是当我使用Python3和
requests
时,结果却是一派胡言。
这是一个很小的脚本,没有显示问题:
#!/usr/bin/env python2
from bs4 import BeautifulSoup
import requests
import mechanize
def main():
# Get lyrics from first website (lyrical-nonsense.com)
url = 'https://www.lyrical-nonsense.com/lyrics/bump-of-chicken/hello-world/'
html_raw_lyrics = BeautifulSoup(requests.get(url).text, "html5lib")
raw_lyrics = html_raw_lyrics.find("div", id="Lyrics").get_text()
# Use second website to anotate lyrics with fugigana
browser = mechanize.Browser()
browser.open('http://furigana.sourceforge.net/cgi-bin/index.cgi')
browser.select_form(nr=0)
browser.form['text'] = raw_lyrics
request = browser.submit()
# My actual script does more stuff at this point, but this snippet doesn't need it
annotated_lyrics = BeautifulSoup(request.read().decode('utf-8'), "html5lib").find("body").get_text()
print annotated_lyrics
if __name__ == '__main__':
main()
截断的输出是:
扉(とびら)開(ひら)けば捻(ねじ)れた昼(ひる)の夜(よる)昨日(きのう)どうやって帰(かえ)った体(からだ)だけが確(たし)かおはよう これからまた迷子(まいご)の続(つづ)き見慣(みな)れた知(し)らない景色(けしき)の中(なか)でもう駄目(だめ)って思(おも)ってから わりと何(なん)だかやれている死(し)にきらないくらいに丈夫(じょうぶ)何(なに)かちょっと恥(は)ずかしいやるべきことは忘(わす)れていても解(わか)るそうしないと とても苦(くる)しいから顔(かお)を上(あ)げて黒(くろ)い目(め)の人(にん)君(くん)が見(み)たから光(ひかり)は生(う)まれた選(えら)んだ色(しょく)で塗(ぬ)った世界(せかい)に [...]
这是一个显示问题的最低限度脚本:
#!/usr/bin/env python3
from bs4 import BeautifulSoup
import requests
def main():
# Get lyrics from first website (lyrical-nonsense.com)
url = 'https://www.lyrical-nonsense.com/lyrics/bump-of-chicken/hello-world/'
html_raw_lyrics = BeautifulSoup(requests.get(url).text, "html5lib")
raw_lyrics = html_raw_lyrics.find("div", id="Lyrics").get_text()
# Use second website to anotate lyrics with fugigana
url = 'http://furigana.sourceforge.net/cgi-bin/index.cgi'
data = {'text': raw_lyrics, 'state': 'output'}
html_annotated_lyrics = BeautifulSoup(requests.post(url, data=data).text, "html5lib")
annotated_lyrics = html_annotated_lyrics.find("body").get_text()
print(annotated_lyrics)
if __name__ == '__main__':
main()
其截断输出为:
IQp{_<n(åiFcf0c_S`QLºKJoFSK~_÷PnMc_åjDorn-gFÄîcfcfKhU`KfD{kMjDOD+UKacheZKWDyMSho،fDfã]FWjDhhfæWDKTRfÒDînºL_KIo~_x`rgWc_Lkò~fxyjD·nsoiS`FTê`QLÒüíüLn [...]
值得注意的是,如果我尝试获取第二个请求的HTML,如下所示:
# Use second website to anotate lyrics with fugigana
url = 'http://furigana.sourceforge.net/cgi-bin/index.cgi'
data = {'text': raw_lyrics, 'state': 'output'}
annotated_lyrics = requests.post(url, data=data).content.decode('utf-8')
打印
embedded null character
时发生
annotated_lyrics
错误。可以通过将截短的歌词传递给post请求来解决此问题。在当前示例中,只能传递一个字符。
然而,与
url = 'https://www.lyrical-nonsense.com/lyrics/aimer/brave-shine/'
我最多可以传递51个字符,如下所示:
data = {'text': raw_lyrics[0:51], 'state': 'output'}
在触发
embedded null character
错误之前。
我尝试使用
urllib
而不是
requests
,解码和编码生成的post请求的HTML,或者作为参数传递给这个请求的
data
。我还检查了网站的编码是否为utf-8,它与post请求的编码匹配:
r = requests.post(url, data=data)
|
|
爽快的四季豆 · XPath 3 Training | Altova 2 月前 |