【问题标题】:Python requests html table inside a iframePython 在 iframe 中请求 html 表
【发布时间】:2020-05-02 23:09:16
【问题描述】:

如何使用请求抓取此链接中的表格?我正在尝试使用请求,但由于表位于 iframe 内,因此 html 返回不完整。 我只需要带有表格的 html,一旦我有了它,我想我可以使用 beatuifulsoup 来处理它。 在我使用的编码下方:

url = 'https://www.rad.cvm.gov.br/ENETCONSULTA/frmGerenciaPaginaFRE.aspx?NumeroSequencialDocumento=89180&CodigoTipoInstituicao=2'
resp = requests.get(url, verify=False)

【问题讨论】:

    标签: python


    【解决方案1】:

    如果你不想使用selenium,你可以使用这个脚本来加载带有requests的表:

    import re
    import requests
    from bs4 import BeautifulSoup
    
    base_url = 'http://www.rad.cvm.gov.br/ENETCONSULTA/frmGerenciaPaginaFRE.aspx?NumeroSequencialDocumento=89180&CodigoTipoInstituicao=2'
    
    # https://stackoverflow.com/questions/38015537/python-requests-exceptions-sslerror-dh-key-too-small
    requests.packages.urllib3.disable_warnings()
    requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS += ':HIGH:!DH:!aNULL'
    try:
        requests.packages.urllib3.contrib.pyopenssl.util.ssl_.DEFAULT_CIPHERS += ':HIGH:!DH:!aNULL'
    except AttributeError:
        # no pyopenssl support used / needed / available
        pass
    
    with requests.session() as s:
        html_data = s.get(base_url, verify=False).text
        url = 'http://www.rad.cvm.gov.br/ENETCONSULTA/' + re.search(r"window\.frames\[0\]\.location='(.*?)'", html_data).group(1)
        soup = BeautifulSoup(s.get(url, verify=False).content, 'html.parser')
    
        print(soup.table.prettify())
    

    打印:

    <table id="ctl00_cphPopUp_tbDados">
     <tr>
      <td style="padding:8px 5px 8px 5px; background:#cccfd1; border-bottom:1px solid #fff !important; text-align:center; color:#ffffff; font:normal normal bold 12px 'Trebuchet MS', sans-serif;">
       Conta
      </td>
      <td style="padding:8px 5px 8px 5px; background:#cccfd1; border-bottom:1px solid #fff !important; text-align:center; color:#ffffff; font:normal normal bold 12px 'Trebuchet MS', sans-serif;">
       Descrição
      </td>
      <td style="padding:8px 5px 8px 5px; background:#cccfd1; border-bottom:1px solid #fff !important; text-align:center; color:#ffffff; font:normal normal bold 12px 'Trebuchet MS', sans-serif;">
       01/07/2019
       <br/>
       a
       <br/>
       30/09/2019
      </td>
    
    ... and so on.
    

    【讨论】:

    • 是否使用了 requests.packages.urllib3.util.ssl.DEFAULT_CIPHERS _ 和 verify = False,因为您位于防火墙后面?
    • @QHarr 显然,这台服务器http://www.rad.cvm.gov.br/ 使用了弱、不安全的密码,所以没有它requests 无法连接。我在 SO 上找到了这个食谱来绕过它。
    【解决方案2】:

    实现这一点的最佳方法是改用 Selenium,等待几秒钟直到 iframe 加载然后捕获 iframe 的内容。

    以下是如何执行此操作的示例:

    import sys
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from time import sleep
    
    url = 'https://www.rad.cvm.gov.br/ENETCONSULTA/frmGerenciaPaginaFRE.aspx?NumeroSequencialDocumento=89180&CodigoTipoInstituicao=2'
    options = Options()
    # activate the following two lines to run in headless mode.
    # options.add_argument('--headless')
    # options.add_argument('--disable-gpu')
    options.add_argument("user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36")
    # /usr/bin/chromedriver is the path where I've installed chromedriver.
    driver = webdriver.Chrome('/usr/bin/chromedriver', chrome_options=options)
    driver.get(url)
    # Wait till iframe loads
    sleep(5)
    html = driver.execute_script("return document.getElementsByTagName('html')[0].innerHTML").encode('utf-8').strip()
    # Now you have the fully-loaded HTML, you may continue to use getElementByTagName or a different library like bs4 to extract the content of the iframe. 
    driver.close()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多