2016-09-12 6 views
0

기계화 된 양식에서 얻은 응답으로 txt 파일을 채우려고합니다. 여기 이것은 문서가 위치에 관해서 얼마나 많은 권한에 따라 HTML 코드의 라인을 뱉어 양식 코드기계화 된 결과의 결과를 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 

는하지만 마지막 줄은 교육의 자신의 전문 분야가 있습니다. BC 주 캐나다의 의사와 상담하십시오.

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

난 당신이 아이디어를 얻을 희망 :

나는 같은 나열된 txt 파일이 있습니다. 다음 단계를 자동화하는 데 도움이 될만한 점이 있으면 고맙겠습니다.

이름이 하나씩있는 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 
+0

아직도 내가 txt 파일로 인쇄 할 수없는 이유를 알 수 없으므로 전문 지식을 빌려주시겠습니까? – Trjjnhrs

+0

당신의'file_out.write (row)'는 어디에 있습니까? – MotKohn

+0

고마워, 나는 당신의 도움으로 다음을 삽입함으로써 이것을 작동시킬 수 있었다 : 's = str (row)' 'file_out.write (s)' – Trjjnhrs

답변

0

너무 복잡하지 않아야합니다.

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) 

이 : 당신이 "names.txt"을 만들려는 출력 파일이라고에 조회 할 모든 이름을 가진 파일을 가정 할 "경우 output.txt"라고, 코드처럼 보일 것이다 파싱 ​​로직이 일종의 "레코드"를 생성하여 파일에 문자열로 작성한다고 가정합니다.

고급 기능을 활용하면 csv 모듈을 통해 CSV로 데이터를 가져 오거나 내보낼 수 있습니다.

또한 Input and Output tutorial을 살펴보십시오.

+0

고맙습니다. – Trjjnhrs