1

I'm new in Python, recently doing a school work request url from Facebook. Then I have to save the result to a text file. Here's my code, and I couldn't figure out how to save the response result. Can anyone help or point a hint? Very much appreciated.

import requests

url = "http://graph.facebook.com/{}/picture?type=large"
default = "https://static.xx.fbcdn.net/rsrc.php/v3/yl/r/HsTZSDw4avx.gif"

for user_id in range(4, 10):
response = requests.get(url.format(str(user_id)))
if response.status_code == requests.codes.ok:
    if response.url != default:
        print(response.url)

f = open("Output.txt", "w+", encoding = "UTF-8")
f.write(response.text)
f.close()
TaurusJ
  • 11
  • 2
  • What is your result, error and expected result? – BrainFl Apr 20 '22 at 03:09
  • 2
    your indentations seem off – drum Apr 20 '22 at 03:09
  • the txt file I saved only showed "http://graph.facebook.com/{}/picture?type=large" or some error message, but not the url. – TaurusJ Apr 20 '22 at 03:15
  • My output: ``` {"error":{"message":"An access token is required to request this resource.","type":"OAuthException","code":104,"fbtrace_id":"AWIyiD4GixOdqxgvtU5cY-Q"}} ``` Requires an access token. You should probably use some other website other than FB if that's a choice. – ask_me Apr 20 '22 at 03:17
  • @ask_me, yes, you r right, that's another outcome I had. Unfortunately, like I said, it's a school work, I have to request those urls form FB. I just can't seem to figure out how to save it into a txt file. – TaurusJ Apr 20 '22 at 03:22
  • 1
    See this: https://stackoverflow.com/questions/4121535/an-access-token-is-required-to-request-this-resource-while-accessing-an-album Check the second answer. What you need is an access token. read up about what access tokens are if you don't know. – ask_me Apr 20 '22 at 03:26
  • 1
    Does this answer your question? ["An access token is required to request this resource" while accessing an album / photo with Facebook php sdk](https://stackoverflow.com/questions/4121535/an-access-token-is-required-to-request-this-resource-while-accessing-an-album) – Code-Apprentice Apr 20 '22 at 03:26
  • 1
    For future reference, googling the error message often leads to solutions to the problem. – Code-Apprentice Apr 20 '22 at 03:27

1 Answers1

2

The issue you're having with saving the response is that you're overwriting it each time. You want to either open your output in 'append' mode

    with open("Output.txt", "a", encoding = "UTF-8") as f:
        f.write(response.url)

Or, construct the desired output and write once:

output = []
for user_id in range(4, 10):
    response = requests.get(url.format(str(user_id)))
    if response.status_code == requests.codes.ok:
        if response.url != default:
            print(response.url)
            output.append(response.url)

with open("Output.txt", "w", encoding = "UTF-8") as f:
    f.write('\n'.join(output))

That being said... I don't think your response is text. Try running the following!

import requests

url = "http://graph.facebook.com/{}/picture?type=large"
default = "https://static.xx.fbcdn.net/rsrc.php/v3/yl/r/HsTZSDw4avx.gif"

output = []
for user_id in range(4, 10):
    response = requests.get(url.format(str(user_id)))
    if response.status_code == requests.codes.ok:
        if response.url != default:
            print(response.url)
            output.append(response.content)

for i, data in enumerate(output):
    with open(f"Output{i}.gif", "wb") as f:
        f.write(data)

One of my output looks like:

fb_default_img

Jupyter Screenshot:

Jupyter Screenshot

BeRT2me
  • 12,699
  • 2
  • 13
  • 31
  • Thank you very much, but the output still not the way I need. I'll keep trying to figure it out, very appreciated. – TaurusJ Apr 20 '22 at 03:31
  • I just made an edit, check it out :) – BeRT2me Apr 20 '22 at 03:33
  • Yep, the response, when successful, is a gif image. You can see my example output where I tell it to display the response as an image. – BeRT2me Apr 20 '22 at 03:53
  • The display content is correct, it's those 4 urls I have to save it to a text file. That's where I got stuck. – TaurusJ Apr 20 '22 at 03:54
  • Okay, if it's the URLs you want, look back to my original response, and instead of `response.text` just do `response.url`~ – BeRT2me Apr 20 '22 at 04:00
  • And next time instead of "save the result", specify "save the urls with successful status codes", and you'll get an answer much faster :') – BeRT2me Apr 20 '22 at 04:03
  • Yeah, I tried that before. The output will only show "http://graph.facebook.com/9/picture?type=large" in the txt file. – TaurusJ Apr 20 '22 at 04:04
  • [image](https://i.stack.imgur.com/gEkTu.png) Try again. – BeRT2me Apr 20 '22 at 04:12
  • OMG! That's the output I was trying to have, thank you so much for the help! :) – TaurusJ Apr 20 '22 at 04:20