- Author:
- msmenzyk
- Posted:
- January 8, 2013
- Language:
- Python
- Version:
- Not specified
- Score:
- 0 (after 0 ratings)
Script saves payment reason codes text from HTML tables to python dict.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | # coding: utf-8
import urllib2
import logging
import codecs
from bs4 import BeautifulSoup
from bs4.element import Tag
from django.core.management.base import NoArgsCommand
log = logging.getLogger(__name__)
class Command(NoArgsCommand):
help = 'Get and parse to python file authorize.net codes from web'
def handle(self, *args, **options):
url = "http://www.authorize.net/support/merchant/Transaction_Response/Response_Reason_Codes_and_Response_Reason_Text.htm"
data = urllib2.urlopen(url).read()
soup = BeautifulSoup(data)
tables = soup.find_all("table")
codes = []
for table in tables:
headings = [th.get_text() if isinstance(th, Tag) else '' for th in table.find_all("tr")[0]]
for row in table.find_all("tr")[1:]:
data = zip(headings, (td.get_text() for td in row.find_all("td")))
c = str(data[1][1]).replace('\n', '')
v = unicode(data[3][1])
if v == ' ': v = ''
codes.append(u"\t'{code}': '{detail}'".format(code=c, detail=v))
output = u"\n#generated\n\n" \
u"RESPONSE_REASON_CODE_DETAILS = {\n" \
u"%s" \
u"\n}" % (',\n'.join([c.replace('\n', '') for c in codes]))
fname = './response_codes.py'
file = codecs.open(fname, "w", "ascii", "ignore" )
file.write(output)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Please login first before commenting.