Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python将网站表单输出的选定部分保存到csv_Python_Csv - Fatal编程技术网

使用python将网站表单输出的选定部分保存到csv

使用python将网站表单输出的选定部分保存到csv,python,csv,Python,Csv,我想在此网站()的表单中输入我的起点和终点(O和D),然后将结果写入csv文件。自动完成这项任务是我唯一的选择,因为要输入的位置接近1000个。使用下面修改的代码,我可以在表单中输入输入位置,并使用br.response().read()将结果打印到我的屏幕上。但是,结果以html格式打印,但我希望下面图像中以蓝色突出显示的部分导出为csv文件。我该怎么做 图像: from mechanize import Browser br = Browser() # Ignore robots.txt

我想在此网站()的表单中输入我的起点和终点(O和D),然后将结果写入csv文件。自动完成这项任务是我唯一的选择,因为要输入的位置接近1000个。使用下面修改的代码,我可以在表单中输入输入位置,并使用
br.response().read()
将结果打印到我的屏幕上。但是,结果以html格式打印,但我希望下面图像中以蓝色突出显示的部分导出为csv文件。我该怎么做

图像:

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???

我的代码:

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???

如果您在Google的Chrome源代码控制台中查看生成的HTML页面的源代码,您将找到四个结果。以下是第一个结果的出发部分的捕获:


通过使用my capture中以黄色突出显示的文本搜索控制台,可以获得剩余的结果。现在您只需使用
Beautiful Soup
对HTML代码进行刮片和切片,然后将切片部分保存到
CSV
文件。

如另一个答案所述,您可以使用HTML解析器(如
Beautiful Soup
)解析响应,选择所需的每个值,将它们放在逗号分隔的字符串中,然后将其写入文件

下面是示例代码,让您更好地了解:

from mechanize import Browser
from bs4 import BeautifulSoup

# get the response from mechanize Browser

soup = BeautifulSoup(response, 'html.parser')
trs = soup.select('table.hfs_overview tr')
with open('out.csv', 'a+') as f:
    for tr in trs:
        locations = tr.select('td.location.departure a')
        if len(locations) > 0:
            location = locations[0].contents[0].strip()
            prefix = tr.select('td.prefix')[0].contents[0].strip()
            time = tr.select('td.time')[0].contents[0].strip()
            # parse more values here
            # write to file
            f.write("{},{},{}\n".format(location, prefix, time))