Parse text response from http request in Python
17,975
Solution 1
request.text
is a property and not a method, request.text
returns a unicode string, request.text()
throws the error 'unicode' object is not callable
.
for line in request.text.splitlines():
print line
Solution 2
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
Related videos on Youtube
Author by
Jitesh
Updated on September 16, 2022Comments
-
Jitesh over 1 year
I am trying to fetch data from an APU but as response I am getting the plain text. I want to read all text line by line.
This is the
url
variable: http://www.amfiindia.com/spages/NAVAll.txt?t=23052017073640First snippet:
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
Result: It prints out the entire text
Second snippet:
request = requests.get(url) for line in request.text(): print line break
Result: It prints out 1 character
request = requests.get(url) requestText = request.text() allMf = requestText.splitlines()
Result: Exception: 'unicode' object is not callable
I have tried few more cases but not able to read text line by line.