Python 使用Beautiful Soup或Selenium(Py)下载ASPX PDF链接

Python 使用Beautiful Soup或Selenium(Py)下载ASPX PDF链接,python,asp.net,selenium,web-scraping,beautifulsoup,Python,Asp.net,Selenium,Web Scraping,Beautifulsoup,我正在尝试清理的站点如下: 它使用ASPX生成指向我想要的PDF的链接 我试图修改的旧代码是: import requests, sys, webbrowser, bs4, os # v1 - this finds links but due to asp does not click through print('Checking for Calendars') res = requests.get('https://imperial.courts.ca.gov/CourtCalendar

我正在尝试清理的站点如下:

它使用ASPX生成指向我想要的PDF的链接

我试图修改的旧代码是:

import requests, sys, webbrowser, bs4, os

# v1 - this finds links but due to asp does not click through
print('Checking for Calendars')
res = requests.get('https://imperial.courts.ca.gov/CourtCalendars/Public/MCalendars.aspx')
res.raise_for_status

soup = bs4.BeautifulSoup(res.text, 'html.parser')

os.makedirs('Calendars', exist_ok=True)

for link in soup.findAll('a', href=True):
    if link.string == 'Misdemeanor':
        linkUrl = 'http:' + link.get('href')

        res = requests.get(linkUrl) # this line is in error because aspx
        #link in html d/n = link after click

        res.raise_for_status()

        pdfFile = open(os.path.join('Calendar', os.path.basename(linkUrl)), 'wb')
        for chunk in res.iter_content(100000):
            pdfFile.write(chunk)
        pdfFile.close
该代码在另一个站点上运行,其中第一页上的链接地址=链接地址,但这里的动态ASPX链接不起作用

我想用按键右键点击每个链接,然后在新的标签“下载”中打开,但这似乎太过分了。(我不知道如何管理Selenium中的几个选项卡。)

有没有一种方法可以简单地下载if循环中的每个链接

我开始的另一个选择是:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Firefox()
browser.get('https://imperial.courts.ca.gov/CourtCalendars/Public/MCalendars.aspx')

# using singular find_element, then click
# this gets one of the links, but not all
# per git, need to use find elements and loop through

#beneath gets 0 new tabs
linkElems = browser.find_elements_by_link_text('Misdemeanor')
totalLinks = len(linkElems)

for i in linkElems:
    i.send_keys(Keys.CONTROL + 't')
但基本上我不确定如何点击和下载(或打开,下载,关闭)每一个


提前感谢。

我打赌它之所以会崩溃,不是因为它是一个ASPX文件,而是因为它是一个相对路径。 如果您这样做,它应该工作:

linkUrl='1〕https://imperial.courts.ca.gov/CourtCalendars/Public/“+link.get('href')

我打赌它之所以崩溃,不是因为它是一个ASPX文件,而是因为它是一个相对路径。 如果您这样做,它应该工作:

linkUrl='1〕https://imperial.courts.ca.gov/CourtCalendars/Public/“+link.get('href')
使用Chrome选项

chromeOptions=webdriver.ChromeOptions()
prefs = {"plugins.always_open_pdf_externally": True}
chromeOptions.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=chromeOptions)
driver.get("https://imperial.courts.ca.gov/CourtCalendars/Public/MCalendars.aspx")

linkElems = driver.find_elements_by_link_text('Misdemeanor')

for i in linkElems:
    driver.get(i.get_attribute('href'))
使用Chrome选项

chromeOptions=webdriver.ChromeOptions()
prefs = {"plugins.always_open_pdf_externally": True}
chromeOptions.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=chromeOptions)
driver.get("https://imperial.courts.ca.gov/CourtCalendars/Public/MCalendars.aspx")

linkElems = driver.find_elements_by_link_text('Misdemeanor')

for i in linkElems:
    driver.get(i.get_attribute('href'))

非常感谢。这似乎可行),但产生了一个新的问题,现在我需要缩短/清理linkUrl以写入Pdfile basename)。但是改进!非常感谢。这似乎可行),但产生了一个新的问题,现在我需要缩短/清理linkUrl以写入Pdfile basename)。但是改进!非常感谢。我遇到了另一个问题,无法使用get请求,所以这救了我。谢谢。我遇到了另一个问题,无法使用get请求,所以这救了我。