小编eri*_*970的帖子

使用Selenium,Chrome和Python下载PDF

我尝试关注有关该主题的先前帖子(例如post 1post 2),但是我仍然很困惑。

我的脚本必须使用一组凭据登录到站点,然后在某些下拉菜单中导航以选择报告。选择报告后,将弹出一个新窗口,必须在其中调整参数以生成报告。设置完参数后,相同的弹出窗口将以生成的PDF格式刷新报告,并使用Chrome的内置PDF查看器显示。我的印象是,将某些选项传递给webdriver会禁用此PDF查看器,而只是下载文件,但是PDF查看器仍在显示,不会自动下载任何内容。我当然会丢失某些东西,或者我写的东西不正确。这是我的代码的要旨:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_experimental_option('prefs',  {
    "download.default_directory": download_dir,
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "plugins.plugins_disabled": ["Chrome PDF Viewer"]
    }
)

browser = webdriver.Chrome(options = chrome_options)

driver = webdriver.Chrome()
driver.get(url)

#In between here are a bunch of steps here that navigates through drop down menus

#This step may not be necessary, but I figured I'd include it to address when the pop up window …
Run Code Online (Sandbox Code Playgroud)

python selenium selenium-chromedriver

10
推荐指数
1
解决办法
4769
查看次数

使用 Python 和 OpenCV 改善图像歪斜校正

我生成的用于检测和纠正偏差的代码给了我不一致的结果。我目前正在开展一个项目,该项目利用图像上的 OCR 文本提取(通过 Python 和 OpenCV),因此如果需要准确的结果,消除倾斜是关键。我的代码用于cv2.minAreaRect检测倾斜。

我使用的图像都是相同的(并且将来也会如此),所以我不确定是什么导致了这些不一致。我在应用代码时添加了两组前后图像(包括来自 的倾斜值cv2.minAreaRect),一组显示成功消除倾斜,另一组显示倾斜未消除(看起来它增加了更多倾斜)。

图 1 之前 ( -87.88721466064453) 图1 之前

图 1 之后(成功校正歪斜) 图 1 之后

图 2 之前 ( -5.766754150390625) 图2 之前

图 2 之后(不成功的相差校正) 图2 之后

我的代码如下。注意:我处理过的图像比此处包含的图像多得多。到目前为止,检测到的偏差始终在 [-10, 0) 或 (-90, -80] 范围内,因此我尝试在代码中解释这一点。

    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img_gray = cv2.bitwise_not(img_gray)
    
    thresh = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
    coords = np.column_stack(np.where(thresh > 0))
    angle = cv2.minAreaRect(coords)[-1] 
      
    if (angle < 0 and angle >= -10):
        angle = -angle #this was intended to undo skew for values in …
Run Code Online (Sandbox Code Playgroud)

python opencv image-processing skew

4
推荐指数
1
解决办法
5693
查看次数