簡體   English   中英

如何從網頁獲取JS重定向的pdf鏈接

[英]How to get a JS redirected pdf linked from a web page

我正在使用獲取網頁的requests ,例如,如下。

import requests
from bs4 import BeautifulSoup
url = "http://www.ofsted.gov.uk/inspection-reports/find-inspection-report/provider/CARE/EY298883"
r = requests.get(url)
soup = BeautifulSoup(r.text)

對於這些頁面中的每個頁面,我都希望獲得標題為“最新報告”部分中指向的第一個pdf文件。 你怎么能配上漂亮的湯呢?

HTML的相關部分是

 <tbody>
 <tr>
          <th scope="col">Latest reports</th>
          <th scope="col" class="date">Inspection <br/>date</th>
          <th scope="col" class="date">First<br/>publication<br/>date</th>
          </tr>
          <tr>
            <td><a href="/provider/files/1266031/urn/106428.pdf"><span class="icon pdf">pdf</span> Early years inspection report </a></td>
            <td class="date">12 Mar 2009</td>
            <td class="date">4 Apr 2009</td>
            </tr>        </tbody>

下面的代碼看起來應該可以,但是不能。

 ofstedbase = "http://www.ofsted.gov.uk" for col_header in soup.findAll('th'): if not col_header.contents[0] == "Latest reports": continue for link in col_header.parent.parent.findAll('a'): if 'href' in link.attrs and link['href'].endswith('pdf'): break else: print '"Latest reports" PDF not found' break print '"Latest reports" PDF points at', link['href'] p = requests.get(ofstedbase+link['href']) print p.content break 

問題是p包含另一個網頁,而不是它應包含的pdf。 有什么方法可以獲取實際的pdf?


更新:

使它與BeautifulSoup的另一個迭代一起工作

 souppage = BeautifulSoup(p.text)
 line = souppage.findAll('a',text=re.compile("requested"))[0]
 pdf = requests.get(ofstedbase+line['href'])

任何更好/更好的解決方案都將不勝感激。

這不是最干凈的解決方案,但是您可以遍歷列標題,直到找到“最新報告”,然后在該表中搜索指向PDF文件的第一個鏈接。

for col_header in soup.findAll('th'):
    if not col_header.contents[0] == "Latest reports": continue
    for link in col_header.parent.parent.findAll('a'):
        if 'href' in link.attrs and link['href'].endswith('pdf'): break
    else:
        print '"Latest reports" PDF not found'
        break
    print '"Latest reports" PDF points at', link['href']
    break

您可以嘗試使用Selenium WebDriver( python -m "easy_install" selenium )自動指示Firefox下載文件。 這需要Firefox:

from selenium import webdriver
from bs4 import BeautifulSoup

profile = webdriver.FirefoxProfile()
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', ('application/pdf'))
profile.set_preference("pdfjs.previousHandler.alwaysAskBeforeHandling", False)
profile.set_preference("browser.helperApps.alwaysAsk.force", False)
profile.set_preference("browser.download.manager.showWhenStarting", False)

driver = webdriver.Firefox(firefox_profile = profile)
base_url = "http://www.ofsted.gov.uk"
driver.get(base_url + "/inspection-reports/find-inspection-report/provider/CARE/EY298883")
soup = BeautifulSoup(driver.page_source)

for col_header in soup.findAll('th'):
    if not col_header.contents[0] == "Latest reports": continue
    for link in col_header.parent.parent.findAll('a'):
        if 'href' in link.attrs and link['href'].endswith('pdf'): break
    else:
        print '"Latest reports" PDF not found'
        break
    print '"Latest reports" PDF points at', link['href']
    driver.get(base_url + link['href'])

該解決方案非常強大,因為它可以完成人類用戶可以做的所有事情,但是它也有缺點。 例如,我試圖解決Firefox提示下載的問題,但對我來說不起作用。 結果可能會有所不同,具體取決於您安裝的加載項和Firefox版本。

使它與BeautifulSoup的另一個迭代一起工作

 souppage = BeautifulSoup(p.text)
 line = souppage.findAll('a',text=re.compile("requested"))[0]
 pdf = requests.get(ofstedbase+line['href'])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM