9

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=23052017073640

First 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.

1
  • Are you sure that in the second snippet you loop over request.text() and not over request.text? And if yes, it should throw an exception because request.text is a property, not a method. Therefore, you don't need () Commented Aug 30, 2017 at 8:03

3 Answers 3

16

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
Sign up to request clarification or add additional context in comments.

Comments

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 Comments

Can you explain your code and why it solves the issue? Code only answers are often not that helpful.
Added the explanation.
0

Try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.