Python: Difference between revisions

452 bytes added ,  26 February 2020
Line 183: Line 183:
Use the requests library to download files and scrape webpages<br>
Use the requests library to download files and scrape webpages<br>
See [https://www.geeksforgeeks.org/get-post-requests-using-python/ Get and post requests in Python]
See [https://www.geeksforgeeks.org/get-post-requests-using-python/ Get and post requests in Python]
====Get Request====
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
import requests
import requests
Line 192: Line 193:
with open("google.html", "wb") as f:
with open("google.html", "wb") as f:
   f.write(req.content)
   f.write(req.content)
</syntaxhighlight>
====Post Request====
<syntaxhighlight lang="python">
data = {'api_dev_key':API_KEY,
        'api_option':'paste',
        'api_paste_code':source_code,
        'api_paste_format':'python'}
 
# sending post request and saving response as response object
r = requests.post(url = API_ENDPOINT, data = data)
 
# extracting response text 
pastebin_url = r.text
print("The pastebin URL is:%s"%pastebin_url)
</syntaxhighlight>
</syntaxhighlight>