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

  • 2
    If its of string type why don't you try replacing it with string replace function. – Varad Mar 23 '17 at 05: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 '17 at 05: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. – Programmer87 Mar 23 '17 at 05: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? – Programmer87 Mar 23 '17 at 05:20
  • 1
    Oh, looks like you need to learn how to represent quotation marks in Python strings first... http://stackoverflow.com/questions/9050355/python-using-quotation-marks-inside-quotation-marks – DYZ Mar 23 '17 at 05:23
  • @DYZ thanks for that article! I will look into it and get back to you. I believe I can utilize the triple quotation tool for this project. – Programmer87 Mar 23 '17 at 05:39
  • @DYZ Yup it works now! – Programmer87 Mar 23 '17 at 06:00

1 Answers1

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.

Varad
  • 920
  • 10
  • 25