2017-05-03 5 views
0

私はいくつかのコードを持っていますが、現在は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 
        answer = browser.response.text 
        #print(answer) 
        soup = BeautifulSoup(answer) 
        #print soup.prettify() 
        status = soup.select('#Table1') 
        print (status) 
        with open('file_output.xls', 'w', 'utf-8') as f: 
         for tag in soup.select("#Table1"): 
         f.write(tag.prettify()) 

は、ここで私はテーブルを解析し、コピーしよう部分です:

# Use BeautifulSoup to parse this data 
       answer = browser.response.text 
       #print(answer) 
       soup = BeautifulSoup(answer) 
       #print soup.prettify() 
       status = soup.select('#Table1') 
       print (status) 
       with open('file_output.xls', 'w', 'utf-8') as f: 
        for tag in soup.select("#Table1"): 
        f.write(tag.prettify()) 

そして、ここでは、私が取得エラーです:

Traceback (most recent call last): 
    File "C:\PROJECT\pdfs\converterpluspa.py", line 90, in <module> 
    with open('file_output.xls', 'w', 'utf-8') as f: 
TypeError: an integer is required 
+0

この 'タグ= tag.encode( 'ASCII'、 '無視')を試みるデコード(。 'ascii') 'ファイルを書く前に –

+0

それは同じエラーをpromped – fsgdfgsd

+0

あなたは' tag'変数の値を表示できますか? –

答えて

1

open()の3番目の引数は、ではなく、bufferingのバッファサイズです。 Pythonの3内の正しいラインは次のようになります

with open('file_output.xls', 'w', encoding='utf-8') as f: 

のPython 2でのようになりつつ:

import codecs 
with codecs.open('file_output.xls', 'w', encoding='utf-8') as f: 
+0

これはうまくいきましたが、file_output.xlsは空白でした。 – fsgdfgsd

+0

開封前の 'print(status)'行は何ですか?それが '[]'の場合、#Table1セレクタと一致するものはありません。 –

+0

私はそれを動作させました。最初は空白がなくなっていましたが、今は他のテーブルから情報を削除して新しいテーブルを作成し続けます。これは私が望むものではありません。あなたが私を助けることができるならば、他のものを削除してください。 – fsgdfgsd

関連する問題