【问题标题】:How to get a JS redirected pdf linked from a web page如何获取从网页链接的 JS 重定向 pdf
【发布时间】:2014-02-09 19:15:18
【问题描述】:

我使用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'])

感激地收到任何更好/更好的解决方案。

【问题讨论】:

    标签: javascript python web-scraping beautifulsoup python-requests


    【解决方案1】:

    这不是最干净的解决方案,但您可以遍历列标题,直到找到“最新报告”,然后在该表中搜索指向 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 版本而异。

    【讨论】:

    • 谢谢!如果我想真正获得 PDF,我想我需要完整的 url,但这只会给我/provider/files/1295389/urn/EY298883.pdf。我只需要添加前缀http://www.ofsted.gov.uk吗?
    • 啊......这并没有给我可悲的 pdf。一定有更复杂的事情发生。
    • 这是因为链接不直接指向 PDF,而是指向随后提供 PDF 的另一个页面。尽管如此,我会看看是否有下载它的好方法。
    • 谢谢。如果这很难,我会​​接受你的问题并单独提出。
    • driver = webdriver.Firefox(firefox_profile = profile) 行为我打开了 Firefox 的副本,然后脚本停止。
    【解决方案2】:

    让它与 BeautifulSoup 的另一个迭代一起工作

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-18
      • 2020-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多