As starting coders, we are busy with a scraping tool in python. It is almost finished, but now we want the result in a JSON file. We tried but it does not work. Is there a code hero who can help us out?
from bs4 import BeautifulSoup
import urllib
jaren = [str("2010"), str("2012")]
DESIRED_COLUMNS = {1, 2, 5} # it is a set
for Jaargetal in jaren:
r = urllib.urlopen("http://www.nlverkiezingen.com/TK" + Jaargetal +".html").read()
soup = BeautifulSoup(r, "html.parser")
tables = soup.find_all("table")
for table in tables:
header = soup.find_all("h1")[0].getText()
print header
trs = table.find_all("tr")[0].getText()
print '\n'
for tr in table.find_all("tr")[:22]:
print "|".join([x.get_text().replace('\n', '')
for index, x in enumerate(tr.find_all('td'))
if index in DESIRED_COLUMNS])
r = urllib.urlopen("http://www.nlverkiezingen.com/TK" + Jaargetal +".html").read(). What should be in that for loop? Should everything underneathfor Jaargetal in jarenbe inside that loop? You should ensure your code is an exact representation of your code that you are runningfor Jaargetal in jaren:. The code is not indented underneath that line.