results17

results17

In [133]:
import requests
import bs4
import getpass

import arrow
In [105]:
myusr = getpass.getuser()
In [2]:
resulreq = requests.get('http://electionresults.govt.nz/')
In [7]:
bssou = bs4.BeautifulSoup(resulreq.text)
/usr/local/lib/python3.4/dist-packages/bs4/__init__.py:181: UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("lxml"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.

The code that caused this warning is on line 170 of the file /usr/lib/python3.4/runpy.py. To get rid of this warning, change code that looks like this:

 BeautifulSoup(YOUR_MARKUP})

to this:

 BeautifulSoup(YOUR_MARKUP, "lxml")

  markup_type=markup_type))
In [36]:
testlis = list()
In [134]:
for sso in (bssou.findAll('select')):
    for sf in (sso.findAll('option')):
#        print(sf)
        testlis.append(sf)
    #print(sso.text)
In [76]:
letes = len(testlis) - 1

want api of each electorate.

name: Auckland Central id: 1 candinates: name: votes, party

In [125]:
 
In [126]:
timnow = arrow.now()
In [132]:
for somerol in range(10,letes):
    reqid = requests.get('http://electionresults.govt.nz/electorate-details-{}.html'.format(somerol))

    reqtxt = bs4.BeautifulSoup(reqid.text, 'lxml')
    
    elebody = (reqtxt.find('tbody'))
    #print(elebody.text)
    try:
        with open('/home/{}/electionresults/posts/results-{}.md'.format(myusr, somerol), 'w') as resulmd:
            resulmd.write(str(somerol) + ' \n' + str(elebody.text))
            
        with open ('/home/{}/electionresults/posts/{}.meta'.format(myusr, somerol), 'w') as opmetat:
                #opmetat.write("{}".format(str(curtim))
            #for arage in alltags:
            #    print(arage)
            opmetat.write('.. title: {}\n.. slug: {}\n.. date: {}\n.. tags: \n.. link:\n.. description:\n.. type: text'.format(somerol, somerol, timnow.for_json()))

        
    except AttributeError: 
        pass
    #     None not in elebody:
    #    with open('/home/pi/memetest/posts/results-{}.md'.format(somerol), 'w') as resulmd:
    #        resulmd.write(str(elebody.text))
    
    
    
In [ ]:
 
In [70]:
#reqid = requests.get('http://electionresults.govt.nz/electorate-details-{}.html'.format(inid))

#reqtxt = bs4.BeautifulSoup(reqid.text, 'lxml')
In [71]:
#elebody = (reqtxt.findAll('tbody'))
In [72]:
#someresul = list()
In [135]:
#for ele in elebody:
    #print(ele.text)
#    someresul.append(ele.text)
In [15]:
#tbodz = bssou.findAll('td', {'class': 'text-left bold'})
In [108]:
bsu = bssou.findAll('td')
In [122]:
with open('/home/{}/electionresults/stories/index.md'.format(myusr), 'w') as stopn:
    for bs in bsu:
        #print(bs.text)
        stopn.write(bs.text)
In [ ]:
#with open('/home/pi/memetest/posts/results-{}.html'.format(inid, 'w') as resulmd:
#    resulmd.write(elebody)
    
    

Comments

Comments powered by Disqus