在Python中解析来自http请求的文本响应

时间:2017-08-30 07:56:19

标签: python python-2.7 parsing

我正在尝试从APU获取数据,但作为响应,我得到了纯文本。我想逐行阅读所有文字。

这是url变量:http://www.amfiindia.com/spages/NAVAll.txt?t=23052017073640

第一个片段:

from pymongo import MongoClient
import requests
from bs4 import BeautifulSoup as bs
url = "https://www.amfiindia.com/spages/NAVAll.txt?t=23052017073640"
request = requests.get(url)
soup = bs(request.text,"lxml")
for line in soup:
    print line
    break

结果:打印出整个文字

第二个片段:

request = requests.get(url)
for line in request.text():
    print line
    break

结果:打印出1个字符

request = requests.get(url)
requestText = request.text()
allMf = requestText.splitlines()

结果:例外:' unicode'对象不可调用

我已经尝试了几个案例,但无法逐行阅读文本。

3 个答案:

答案 0 :(得分:1)

request.text是一个属性而不是方法,request.text返回一个unicode字符串,request.text()会抛出错误'unicode' object is not callable

for line in request.text.splitlines():
    print line

答案 1 :(得分:1)

import requests
from bs4 import BeautifulSoup as bs
url = "https://www.amfiindia.com/spages/NAVAll.txt?t=23052017073640"
request = requests.get(url)
soup = bs(request.text,"lxml")

# soup.text is to get the returned text
# split function, splits the entire text into different lines (using '\n') and stores in a list. You can define your own splitter.
# each line is stored as an element in the allLines list.
allLines = soup.text.split('\n') 

for line in allLines: # you iterate through the list, and print the single lines
    print(line)
    break # to just print the first line, to show this works

答案 2 :(得分:0)

试试这个:

from pymongo import MongoClient
import requests
from bs4 import BeautifulSoup as bs
url = "https://www.amfiindia.com/spages/NAVAll.txt?t=23052017073640"
request = requests.get(url)
soup = bs(request.text,"lxml")
for line in soup:
    print line.text
    break