Downloading an on online report using Python requests

牧云@^-^@ 提交于 2020-06-17 09:15:28

问题


How could you request the following curl command using Python please to download a report from Ads Manager (Facebook)?

curl "https://scontent-mad1-1.xx.fbcdn.net/v/t66.9516-6/reportid.csv/reportname.csv?_nc_cat=109^&_nc_sid=7a48f7^&_nc_ohc=m4lvbyCnNzsAX9VwD8m^&_nc_ad=z-m^&_nc_cid=0^&_nc_ht=scontent-mad1-1.xx^&oh=45e8967402708665322afdb82bad4a46^&oe=5F0D314B" ^
  -H "authority: scontent-mad1-1.xx.fbcdn.net" ^
  -H "upgrade-insecure-requests: 1" ^
  -H "user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36" ^
  -H "accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" ^
  -H "sec-fetch-site: cross-site" ^
  -H "sec-fetch-mode: navigate" ^
  -H "sec-fetch-dest: iframe" ^
  -H "referer: https://business.facebook.com/" ^
  -H "accept-language: en-GB,en-US;q=0.9,en;q=0.8" ^
  --compressed

Do you know how to fix the following python request to download the report?

#!/usr/bin/env python3
import requests

url = "https://scontent-mad1-1.xx.fbcdn.net/v/t66.9516-6/<reportid.csv>/<reportname.csv>?_nc_cat=109^&_nc_sid=7a48f7^&_nc_ohc=m4lvbyCnNzsAX9VwD8m^&_nc_ad=z-m^&_nc_cid=0^&_nc_ht=scontent-mad1-1.xx^&oh=45e8967402708665322afdb82bad4a46^&oe=5F0D314B"

#headers to Python dictionary
headers = {"authority": "scontent-mad1-1.xx.fbcdn.net", "upgrade-insecure-requests": "1",
       "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36", "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", "sec-fetch-site": "cross-site", "sec-fetch-mode": "navigate", "sec-fetch-dest": "iframe", "referer": "https://business.facebook.com/", "accept-language": "en-GB,en-US;q=0.9,en;q=0.8"}

response = requests.get(url, headers=headers)

In which folder would the report be downloaded?

Do I need to enter the Facebook user and password since I need to be logged in Facebook to download it? And if so, how would I do that?

Could Facebook block my account for executing this request?

I appreciate your help. Thank you!


回答1:


Welcome back. Each -H corresponds to a header, so for example, you would do:

#!/usr/bin/env python3
import requests

url = "https://scontent-mad1-1.xx.fbcdn.net/v/t66.9516-6/reportid.csv/reportname.csv?_nc_cat=109^&_nc_sid=7a48f7^&_nc_ohc=m4lvbyCnNzsAX9VwD8m^&_nc_ad=z-m^&_nc_cid=0^&_nc_ht=scontent-mad1-1.xx^&oh=45e8967402708665322afdb82bad4a46^&oe=5F0D314B"

# don't forget to add all the rest
headers = {"upgrade-insecure-requests": "1",
           "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"}

response = requests.get(url, headers=headers)


来源:https://stackoverflow.com/questions/62398377/downloading-an-on-online-report-using-python-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!