-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Can make web requests including 'get', 'post', 'put' #1
- Loading branch information
1 parent
df6034b
commit fc9eec7
Showing
3 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
import generator | ||
import generator, web |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
import requests | ||
|
||
""" | ||
Defining this function as of now. Will come back to this later. Need to have a generic function for all the scrape related actions | ||
""" | ||
|
||
|
||
def scrape(url): # type can be JSON as of now | ||
scraped_webpage = requests.get(url) | ||
if type != "JSON": | ||
return scraped_webpage.text | ||
try: | ||
return scraped_webpage.json() | ||
except ValueError: | ||
return 'No JSON object could be decoded' | ||
|
||
|
||
def request(url, **kwargs): | ||
if kwargs is not None: | ||
try: | ||
payload = kwargs['params'] | ||
except KeyError: | ||
payload = None | ||
return 'It only supports optional argument: method, params and type' | ||
try: | ||
method = kwargs['method'] | ||
except KeyError: | ||
print 'Please mention the method: get, post, put' | ||
return | ||
|
||
try: | ||
return_type = kwargs['type'] | ||
except KeyError: | ||
return_type = None | ||
|
||
if method.lower() == "get": | ||
r = requests.get(url, params=payload) | ||
elif method.lower() == "post": | ||
r = requests.post(url, params=payload) | ||
elif method.lower() == "put": | ||
r = requests.put(url, params=payload) | ||
else: | ||
return 'Method not allowed' | ||
|
||
if return_type == "JSON": | ||
return r.json() | ||
else: | ||
return r.text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
urllib3==1.22 | ||
Faker==0.8.4 | ||
requests==2.18.4 |