Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added XML response to web.py #26

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions hackr/web.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import requests
import json
from xmljson import badgerfish as bf
from xml.etree.ElementTree import tostring

import requests
from xmljson import badgerfish as bf
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xmljson needs to be mentioned in the requirements file. Can you please update it?


"""
Defining this function as of now. Will come back to this later.
Need to have a generic function for all the scrape related actions.
Expand All @@ -16,21 +17,21 @@ def scrape(url, **kwargs): # type can be JSON as of now
scraped_webpage = requests.get(url)
return_type = kwargs['type'].lower()
if return_type == "json":
try:
return scraped_webpage.json()
except ValueError:
return 'No JSON object could be decoded'
try:
return scraped_webpage.json()
except ValueError:
return 'No JSON object could be decoded'
elif return_type == "xml":
try:
json_data = json.loads(scraped_webpage.content)
xml_data = ""
for element in bf.etree(json_data):
xml_data+=tostring(element)
return xml_data
except ValueError:
return 'No XML could be decoded'
try:
json_data = json.loads(scraped_webpage.content)
xml_data = ""
for element in bf.etree(json_data):
xml_data += tostring(element)
return xml_data
except ValueError:
return 'No XML could be decoded'
else:
return scraped_webpage.text
return scraped_webpage.text


def request(url, **kwargs):
Expand Down