Advertisement
truong3i2

Python - Web Scraping with POST method

Feb 12th, 2019
1,305
-1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 1
  1. # importing the requests library
  2. import requests
  3.  
  4. # defining the api-endpoint
  5. API_ENDPOINT = "http://pastebin.com/api/api_post.php"
  6.  
  7. # your API key here
  8. API_KEY = "XXXXXXXXXXXXXXXXX"
  9.  
  10. # your source code here
  11. source_code = '''
  12. print("Hello, world!")
  13. a = 1
  14. b = 2
  15. print(a + b)
  16. '''
  17.  
  18. # data to be sent to api
  19. data = {'api_dev_key':API_KEY,
  20.         'api_option':'paste',
  21.         'api_paste_code':source_code,
  22.         'api_paste_format':'python'}
  23.  
  24. # sending post request and saving response as response object
  25. r = requests.post(url = API_ENDPOINT, data = data)
  26.  
  27. # extracting response text
  28. pastebin_url = r.text
  29. print("The pastebin URL is:%s"%pastebin_url)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement