Looping variables to a url

by: ianweb1000, 6 years ago


Im trying to deploy a pandas data frame to a URL and get the return from some fields in new columns in the same data frame.

The data frame looks like this :

    name                state postcode VIC     NSW   ACT  NT
    coles                  VIC                2501      Y        N   N N
    woolworth          NSW        3409      N         Y N N
    big w                  AC                3201      N       N Y N
    target                  NT                1089      N         N N Y

I want the name of my columns per row to be deployed and the results in new columns as the example show below. The code below works but I can only make work manually.

    import urllib.request as req

    name = ''
    postcode = ''
    legalName = ''
    tradingName = ''
    NSW = ''
    SA = ''
    ACT = ''
    VIC = ''
    WA = ''
    NT = ''
    QLD = ''
    TAS = ''
    authenticationGuid = 'b6aaddd4-8463-4d76-9e24-99530f5df326' #Your GUID should go here

    #Constructs the URL by inserting the search parameters specified above
    #GETs the url (using urllib.request.urlopen)
    conn = req.urlopen('http://abr.business.gov.au/abrxmlsearchRPC/AbrXmlSearch.asmx/' +
'ABRSearchByNameSimpleProtocol?name=' + name +
'&postcode=' + postcode + '&legalName=' + legalName +
'&tradingName=' + tradingName + '&NSW=' + NSW +
'&SA=' + SA + '&ACT=' + ACT + '&VIC=' +  VIC +
'&WA=' + WA + '&NT=' + NT + '&QLD=' + QLD +
'&TAS=' + TAS + '&authenticationGuid=' + authenticationGuid)

    #XML is returned by the webservice
    #Put returned xml into variable 'returnedXML'
    #Output xml string to file 'output.xml' and print to console
    returnedXML = conn.read()
    f = open('output.xml', 'wb')
    f.write(returnedXML)
    f.close
    print(returnedXML)





You must be logged in to post. Please login or register an account.




for name, row in dataframe.iterrows():
    conn = req.urlopen('http://abr.business.gov.au/abrxmlsearchRPC/AbrXmlSearch.asmx/' +
'ABRSearchByNameSimpleProtocol?name=' + name +
'&postcode=' + row['postcode'])
    returnedXML = conn.read()
    f = open('output.xml', 'wb')
    f.write(returnedXML)
    f.close
    print(returnedXML)

I hope this can help you and here are some advice to you, too.
First, you can try the package requests, it can handle HTTP request more easily.
Then, you may need to learn some thing about string format, which can help you out of the ugly code of linking tons of string.

-yangmqglobe 6 years ago

You must be logged in to post. Please login or register an account.