如何使用机械化结果填充txt?

时间:2016-09-12 04:28:55

标签: python mechanize

我正在尝试使用机械化表单中的响应来填充txt文件。这是表单代码

import mechanize
from bs4 import BeautifulSoup
br = mechanize.Browser()
br.open ('https://www.cpsbc.ca/physician_search')

first = raw_input('Enter first name: ')
last = raw_input('Enter last name: ')

br.select_form(nr=0)
br.form['filter[first_name]'] = first
br.form['filter[last_name]'] = last
response = br.submit()
content = response.read()
soup = BeautifulSoup(content, "html.parser")

for row in soup.find_all('tbody'):
    print row

根据doc对位置的权限,这会喷出html代码行,但最后一行有他们的培训专长。请继续前往加拿大不列颠哥伦比亚省的任何医生进行测试。

我有一个如下所示的txt文件:

lastname1, firstname1
lastname2, firstname2
lastname3, firstname3 middlename3
lastname4, firstname4 middlename4

我希望你明白这个主意。我希望在自动化以下步骤方面有任何帮助:

逐个浏览带有名称的txt,并将输出文本记录到新的txt文件中。

到目前为止,我有这个工作来吐出行(这是原始的html),我不介意,但我不能把它写入一个txt文件......

import mechanize
from bs4 import BeautifulSoup

with open('/Users/s/Downloads/hope.txt', 'w') as file_out:
    with open('/Users/s/Downloads/names.txt', 'r') as file_in:
        for line in file_in:
            a = line
            delim = ", "
            i1 = a.find(delim)

            br = mechanize.Browser()
            br.open('https://www.cpsbc.ca/physician_search')

            br.select_form(nr=0)
            br.form['filter[first_name]'] = a[i1+2:]
            br.form['filter[last_name]'] = a[:i1]
            response = br.submit()
            content = response.read()
            soup = BeautifulSoup(content, "html.parser")

            for row in soup.find_all('tbody'):
                print row

1 个答案:

答案 0 :(得分:0)

这不应该太复杂。假设您的文件包含您要查询的所有名称,并调用" names.txt"您要创建的输出文件名为" output.txt",代码应如下所示:

with open('output.txt', 'w') as file_out:
    with open('names.txt', 'r') as file_in:
        for line in file_in:
            <your parsing logic goes here>
            file_out.write(new_record)

这假设您的解析逻辑会生成某种&#34;记录&#34;作为字符串写在文件上。

如果您获得更高级功能,您还可以查看csv模块以CSV格式导入/导出数据。

另请查看Input and Output tutorial