I want to enter my origin and destination(O and D) into a form on this website (http://fahrplan.sbb.ch/bin/query.exe/en), then write the result to csv file. Automating this task is the only option I have since the number of locations to enter are close to 1000. Using the code below which I modified from here, I am able to input the entry locations in the form and print the result to my screen with br.response().read()
. However, the result is printed in html format but I want the portion highlighted in blue in the image below exported to a csv file. How can I do that?
Image:
My Code:
from mechanize import Browser
br = Browser()
# Ignore robots.txt
br.set_handle_robots( False )
# Google demands a user-agent that isn't a robot
br.addheaders = [('User-agent', 'Chrome')]
# Retrieve the Google home page, saving the response
br.open('http://fahrplan.sbb.ch/bin/query.exe/en')
# # Show the available forms
# counter = 0
# for f in br.forms():
# counter += 1
# print f, counter
# print 'counter', counter
# Enter the text inpur
br.select_form(nr=6)
br.form[ "REQ0JourneyStopsS0G" ] = 'Leverkusen Mitte'
br.form[ "REQ0JourneyStopsZ0G" ] = 'Pescara Centrale'
# Get the search results
br.submit()
print br.response().read()
# How can I export the result to csv???