Login

Grab RESPONSE_CODES from Authorize.net html page to python file

Author:
msmenzyk
Posted:
January 8, 2013
Language:
Python
Version:
Not specified
Tags:
python bs4
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

  1. NoneWidget by diverman 4 years, 9 months ago
  2. Whore links by nathan 8 years, 1 month ago
  3. {% exec %} template tag by aquila 8 years ago
  4. Strip html comments middleware by trbs 8 years, 1 month ago
  5. HTML Widgets by robhudson 7 years, 1 month ago

Comments

Please login first before commenting.