【问题标题】:Scraping multiple html tables with the same URL but different tag使用相同的 URL 但不同的标签抓取多个 html 表
【发布时间】:2020-10-26 21:22:29
【问题描述】:

我正在尝试抓取网页https://www.bolsadeproductos.cl/pagador/20 以获得底部的表格。但是在使用下一个代码时,我无法获得所有结果,只有前 10 行。如何循环遍历所有不同的标签?

from selenium import webdriver
from bs4 import BeautifulSoup
import time
import pandas as pd

driver = webdriver.Edge('C:\\Users\\facun\\Documents\\msedgedriver.exe')
driver.get('https://www.bolsadeproductos.cl/pagador/20')

df = pd.read_html(driver.page_source, attrs = {'id': 'tbl_export'})

提前谢谢你。

【问题讨论】:

    标签: python web-scraping beautifulsoup html-table


    【解决方案1】:

    数据是通过JavaScript动态加载的,但是你可以使用requests模块来获取结果:

    import requests
    from bs4 import BeautifulSoup
    
    
    url = 'https://www.bolsadeproductos.cl/pagador/tablePagador/20/undefined/0'
    
    soup = BeautifulSoup(requests.get(url).content, 'html.parser')
    for i, row in enumerate(soup.select('tr:has(td)'), 1):
        row = [td.get_text(strip=True) for td in row.select('td')]
        print('{:<5}{:<15}{:<15}{:<10}{:<10}{:<20}{:<15}{:<15}'.format(i, *row))
    

    打印:

    1    30-06-2015     FANGLOS        LV        LV        $ 26.586.879        0,34%          30             
    2    26-06-2015     FANGLOS        LV        LV        $ 26.574.872        0,34%          34             
    3    27-05-2015     FANGLOS        LV        LV        $ 1.059.184.359     0,34%          16             
    4    16-06-2015     FANGLOS        LV        LV        $ 996.461.527       0,34%          37             
    5    16-06-2015     FANGLOS        LV        LV        $ 996.461.527       0,34%          37             
    6    27-05-2015     FANGLOS        LV        LV        $ 1.059.184.359     0,34%          16             
    
        
    ... all the way to:
    
    309  23-12-2019     FANGLOS        LV        BCI       $ 193.475.303       0,26%          56             
    310  03-03-2020     FANGLOS        LV        LV        $ 8.558.358         0,26%          13             
    311  06-03-2020     FANGLOS        LV        LV        $ 8.560.581         0,26%          10             
    312  06-03-2020     AANGLOS        LV        LV        $ 63.596.531        0,26%          59             
    313  06-03-2020     FANGLOS        LV        BCI       $ 45.678.549        0,26%          31             
    314  19-05-2020     FANGLOS        BCI       BCI       $ 849.422.583       0,22%          17             
    

    【讨论】:

      【解决方案2】:

      这很简单,只需在您的代码中替换这一行:

      driver.get('https://www.bolsadeproductos.cl/pagador/20')
      

      driver.get('https://www.bolsadeproductos.cl/pagador/tablePagador/20/undefined/0')
      
      df = pd.read_html(driver.page_source, attrs = {'id': 'tbl_export'})
      print(df)
      
      

      输出:

      [    Fecha Operacion Nemotecnico Vendedor Comprador            Monto   Tasa  Plazo(Dias)
      0        30-06-2015     FANGLOS       LV        LV     $ 26.586.879  0,34%           30
      1        26-06-2015     FANGLOS       LV        LV     $ 26.574.872  0,34%           34
      2        27-05-2015     FANGLOS       LV        LV  $ 1.059.184.359  0,34%           16
      3        16-06-2015     FANGLOS       LV        LV    $ 996.461.527  0,34%           37
      4        16-06-2015     FANGLOS       LV        LV    $ 996.461.527  0,34%           37
      ..              ...         ...      ...       ...              ...    ...          ...
      309      03-03-2020     FANGLOS       LV        LV      $ 8.558.358  0,26%           13
      310      06-03-2020     FANGLOS       LV        LV      $ 8.560.581  0,26%           10
      311      06-03-2020     AANGLOS       LV        LV     $ 63.596.531  0,26%           59
      312      06-03-2020     FANGLOS       LV       BCI     $ 45.678.549  0,26%           31
      313      19-05-2020     FANGLOS      BCI       BCI    $ 849.422.583  0,22%           17
      

      【讨论】:

      • 只是问一下,我如何知道下次我需要更改的 url 是什么?在这种情况下,“bolsadeproductos.cl/pagador/20”对应“bolsadeproductos.cl/pagador/tablePagador/20/undefined/0”。我的问题是你怎么知道这个网址会起作用?
      • 您需要开始检查 google chrome (ctrl + shift + i) 中的元素,然后导航到“网络”选项卡并检查所有响应。尝试找到所需的文本,然后您就会知道需要使用哪个url。
      • 非常感谢你,罗曼。这对我很有帮助。
      【解决方案3】:

      看起来这个网站使用了一个 javascript 加载器。查看Selenium Waits 等待页面完全加载。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-21
        • 2018-11-12
        • 1970-01-01
        • 2023-01-11
        • 1970-01-01
        • 2023-03-23
        • 2012-06-20
        • 2021-04-13
        相关资源
        最近更新 更多