2017-05-02 4 views
0

RoboBrowser를 사용하여 웹 사이트에 많은 요청을 보내고 답변을 얻는 프로그램이 있는데, 이제는이 문자열을 가지고 있지 않은 답변 만 필터링해야합니다. "케이스 상태를 사용할 수 없습니다."beautifulsoup를 사용하려고했지만 오류가 반환됩니다. 이다 반환브라우저 응답을 만드는 방법

import shlex 
import subprocess 
import os 
import platform 
from bs4 import BeautifulSoup 
import re 
import csv 
import pickle 
import requests 
from robobrowser import RoboBrowser 

def rename_files(): 
    file_list = os.listdir(r"C:\\PROJECT\\pdfs") 
    print(file_list) 
    saved_path = os.getcwd() 
    print('Current working directory is '+saved_path) 
    os.chdir(r'C:\\PROJECT\\pdfs') 
    for file_name in file_list: 
     os.rename(file_name, file_name.translate(None, " ")) 
    os.chdir(saved_path) 
rename_files() 

def run(command): 
    if platform.system() != 'Windows': 
     args = shlex.split(command) 
    else: 
     args = command 
    s = subprocess.Popen(args, 
         stdout=subprocess.PIPE, 
         stderr=subprocess.PIPE) 
    output, errors = s.communicate() 
    return s.returncode == 0, output, errors 

# Change this to your PDF file base directory 
base_directory = 'C:\\PROJECT\\pdfs' 
if not os.path.isdir(base_directory): 
    print "%s is not a directory" % base_directory 
    exit(1) 
# Change this to your pdf2htmlEX executable location 
bin_path = 'C:\\Python27\\pdfminer-20140328\\tools\\pdf2txt.py' 
if not os.path.isfile(bin_path): 
    print "Could not find %s" % bin_path 
    exit(1) 
for dir_path, dir_name_list, file_name_list in os.walk(base_directory): 
    for file_name in file_name_list: 
     # If this is not a PDF file 
     if not file_name.endswith('.pdf'): 
      # Skip it 
      continue 
     file_path = os.path.join(dir_path, file_name) 
     # Convert your PDF to HTML here 
     args = (bin_path, file_name, file_path) 
     success, output, errors = run("python %s -o %s.html %s " %args) 
     if not success: 
      print "Could not convert %s to HTML" % file_path 
      print "%s" % errors 
htmls_path = 'C:\\PROJECT' 
with open ('score.csv', 'w') as f: 
    writer = csv.writer(f) 
    for dir_path, dir_name_list, file_name_list in os.walk(htmls_path): 
     for file_name in file_name_list: 
      if not file_name.endswith('.html'): 
       continue 
      with open(file_name) as markup: 
       soup = BeautifulSoup(markup.read()) 
       text = soup.get_text() 
       match = re.findall("PA/(\S*)", text)#To remove the names that appear, just remove the last (\S*), to add them is just add the (\S*), before it there was a \s* 
       print(match) 
       writer.writerow(match) 
       for item in match: 
        data = item.split('/') 
        case_number = data[0] 
        case_year = data[1] 

        browser = RoboBrowser() 
        browser.open('http://www.pa.org.mt/page.aspx?n=63C70E73&CaseType=PA') 
        form = browser.get_forms()[0] # Get the first form on the page 
        form['ctl00$PageContent$ContentControl$ctl00$txtCaseNo'].value = case_number 
        form['ctl00$PageContent$ContentControl$ctl00$txtCaseYear'].value = case_year 

        browser.submit_form(form, submit=form['ctl00$PageContent$ContentControl$ctl00$btnSubmit']) 

        # Use BeautifulSoup to parse this data 
        print(browser.response.text) 
        souptwo = BeautifulSoup(browser.response.text) 
        texttwo = soup.get_text() 
        matchtwo = soup.findall('<td class="fieldData">Case Status Not Available</TD>') 
        if not matchtwo: 
         soupthree = BeautifulSoup(browser.response.text) 
         print soupthree 

오류 :

Traceback (most recent call last): 
    File "C:\PROJECT\pdfs\converterpluspa.py", line 87, in <module> 
    matchtwo = soup.findall('<td class="fieldData">Case Status Not Available</TD>') 
TypeError: 'NoneType' object is not callable 

답변

0

라인 87이 방법을 soupfindall를 호출하는 시도를 포함

다음은 지금까지 코드입니다. soup은 파일 내용을 구문 분석하기 위해 BeautifulSoup이 호출 된 65 행에 정의되었습니다. 오류 진단에서 soup이 없음이라고 표시하면 BeautifulSoup이 해당 파일을 구문 분석 할 수 없음을 의미합니다.

+0

감사합니다. 어떻게 수정해야합니까? – fsgdfgsd

+0

BeautifulSoup에 제공하는 파일의 내용이 유효한 HTML인지 확신하십니까? 시험을 위해 100 자 정도의 문자를 인쇄해야합니다. –

+0

그래, 나는 프로그램을 재정비 할 수 있었고 지금은 거의 작동하고있다. 이제 나는 http://www.pa.org.mt/SearchPA와 같은 많은 링크에서 얻은 정보를 바탕으로 테이블을 만들 필요가있다. 테이블을 가져와야합니다 : 현재 응용 프로그램 상태 및 모든 테이블 아래에 그것을 작성하고 간단한 단일 엑셀 테이블에 씁니다. 그러나 무엇을 얻는지는 테이블의 사본입니다. – fsgdfgsd