url - 用Python从网页提取的文本,转换成str格式后,怎么逐行处理呢?
迷茫
迷茫 2017-04-18 09:06:25
0
2
405

用Python从url上抓取文本之后,想对文本进行逐行的解析,老出错,debug时发现每一行都是一个字母,不是一句完整的话。想不太明白,请大神指教。 fhand 和 line 的 type 都是 str

import urllib.request

fhand = urllib.request.urlopen('http://www.pythonlearn.com/code/mbox.txt').read().decode()
a=0
print (fhand)

for line in fhand:

if a < 5:
    print (type(line) )
    print (line)
    a=a+1
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

répondre à tous(2)
小葫芦
fhand = urllib.request.urlopen('http://www.pythonlearn.com/code/mbox.txt').readlines()

for line in fhand:
  # ...

或者:

fhand = urllib.request.urlopen('http://www.pythonlearn.com/code/mbox.txt').read().decode()

for line in fhand.split('\n'):
  # ...
Ty80
for line in Ghana.split(“\n”)
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!