需要帮助制作一个程序,该程序利用存储在变量中的信息进入网站



我当前正在制作一个将PDF转换为HTML的程序,现在我需要输入我从HTML文件中删除的刮擦数据这是网站:http://www.pa.org.mt/page.aspx?n=63c70e73&casetype = pa

这就是我到目前为止得到的:

import shlex
import subprocess
import os
import platform
from bs4 import BeautifulSoup
import re
import csv
import pickle
import requests
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)
                value = match
                url = 'http://www.pa.org.mt/page.aspx?n=63C70E73&CaseType=PA'
                query = {'field': value}
                res = requests.post(url, data=query)

我喜欢您的项目想法,我可能会自己尝试!无论如何,我建议您使用硒。我访问了您的网站,然后编写了代码,将数据输入第一个文本框(情况类型(和第二个文本框(案例号(,然后按Enter。但是,您必须先安装硒,这应该非常容易。为Windows安装硒:

  • 获取在笔记本电脑上安装PIP的确切路径。
  • 打开CMD并在路径中粘贴。然后添加以下内容:"安装硒"。在将其键入
  • 之前,请确保您在路径之后离开空间
  • 硒应成功安装。现在,您必须安装WebDriver。访问:https://pypi.python.org/pypi/selenium
  • 向下滚动到驱动程序部分,并为浏览器安装适当的驱动程序。
  • 将下载的文件与您的代码相同的位置。

就是这样。现在,您可以在代码中需要复制并粘贴以下内容:

import time, sys
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
Chrome=  webdriver.Chrome()
Chrome.get("http://www.pa.org.mt/page.aspx?n=63C70E73&CaseType=PA")
casetype = Chrome.find_element_by_name("ctl00$PageContent$ContentControl$ctl00$txtCaseNo")
casenumber = Chrome.find_element_by_name("ctl00$PageContent$ContentControl$ctl00$txtCaseYear")
submit = Chrome.find_element_by_name("ctl00$PageContent$ContentControl$ctl00$btnSubmit")
casetype.send_keys("Hi")
casenumber.send_keys("Hey")
submit.send_keys(Keys.RETURN)
time.sleep(3)

正如已经说的那样,这将填写前两个文本框,然后按第一个提交。该代码通过查找您需要的特定文本框来实现此目的:

txtbx1 = Chrome.find_element_by_name("ctl00$PageContent$ContentControl$ctl00$txtCaseNo")

然后,它使用:

发送您需要发送给它的任何数据
txtbx1.send_keys("Hi")

等等。您可以使用此信息来修改代码并准确执行您想要的操作。例如,如果要填写该页面上的第三个文本框,请添加上面的2行,替换括号中的内容,用文本框的名称。

ps:要查找文本框的名称,请访问您的网站,右键单击文本框,单击"检查",然后找到SAIS"名称"的零件,然后在Qoutes中复制内容。就是这样。如果有一些您不了解的东西或您想知道的东西,请在下面发表评论。最后,由于您使用了美丽的汤和要求,这可能不适合您,但我向您保证它仍然有效!

最新更新