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

用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
迷茫
迷茫

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

Antworte allen (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”)
      Neueste Downloads
      Mehr>
      Web-Effekte
      Quellcode der Website
      Website-Materialien
      Frontend-Vorlage
      Über uns Haftungsausschluss Sitemap
      Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!