0

I am executing the follow piece of code:

def do_scores(self,arg,opts = None):
    r = requests.get("https://www.reddit.com/r/nba/")
    response = r.text;
    print(cresponse).

What this is doing, is making a simple get request to the website provided above and I wish to store this in a string, which I will go on to manipulate in my code afterwards. However, I am unable to do this because the response string has numerous occassions where it contains a " and therefore messes up parsing of the string. Therefore I was wondering if there was any way to replace all of these " with a different character such as / or '.

7
  • 2
    If its of string type why don't you try replacing it with string replace function.
    – Varad
    Mar 23, 2017 at 5:12
  • First, your code fragment is incorrect (cresponse must be responce). Second, those quotation marks are a part of the HTML code, and removing them, in fact, may make parsing impossible. Assuming that you use BeautifulSoup or a similar parser, let them do the job!
    – DYZ
    Mar 23, 2017 at 5:16
  • @Varad I tried using that, but it seems to be impossible because the first argument I need to pass for that command is the " itself, and it is not allowing that. Mar 23, 2017 at 5:18
  • @DYZ hmm sorry was kind of shifting around some of the code to make it readable and didn't catch that typo! But what do you recommend then? The response is definitely a proper string, so I should use some external module to therefore remove the quotation marks? The quotation marks are not relevant in gathering the information I want from that string at all, therefore that should not be too much of a problem right? Mar 23, 2017 at 5:20
  • 1
    Oh, looks like you need to learn how to represent quotation marks in Python strings first... stackoverflow.com/questions/9050355/…
    – DYZ
    Mar 23, 2017 at 5:23

1 Answer 1

0

I believe you need to perform the replacement in the response and not on request.

You can get the response using response.text which gives you an output like u'{"type":"User"...' and you can also get the response like response.json and you get the output in the format {u'disk_usage': 368627, u'private_gists': 484, ...}.

If you use response.json then I think you an extract the needed values using get or `

import json
json.dumps(response.json)

You can have more options is what I believe if you take the response in json format.

Not the answer you're looking for? Browse other questions tagged or ask your own question.