簡體   English   中英

無法用漂亮的湯刮桌子

[英]Unable to scrape some table using beautiful soup

我正在使用 BeautifulSoup 從網站鏈接中抓取數據。 共有 9 個表具有相同的 class 名稱,但我只能獲得鏈接 5 個表。 我應該在代碼中做哪些更改,以便我可以從上面的鏈接中抓取所有存在的表格?

以下是我使用的代碼:

def ScrapeSecScreen():
    options = webdriver.ChromeOptions()
    options.add_argument('--ignore-certificate-errors')
    options.add_argument('--incognito')
    options.add_argument('--headless')
    driver = webdriver.Chrome("C:/Users/pralo/Downloads/DE/chromedriver", chrome_options=options)
    driver.get('https://www.panamacompra.gob.pa/Inicio/v2/#!/vistaPreviaCP?NumLc=2022-0-30-0-08-CL-024792&esap=0&nnc=0&it=1')
    sleep(10)
    sourcecode = driver.execute_script("return document.getElementsByTagName('html')[0].innerHTML")
    # print(sourcecode)
    soup = BeautifulSoup(sourcecode,"html.parser")
    print(soup)
    l1 = []
    tablelist1=soup.findAll('table',{'class':'table table-condensed table-bordered last-line-table'})
    for tr in tablelist1:
        td = tr.find_all('tr')
        row = [tr.text for tr in td]
        l1.append(row)
    print(l1)
ScrapeSecScreen()

經檢查,事實證明只有前五個表與您提到的類匹配。 要獲取所有表,您可以省略 class 參數或指定所有表共享的 class。

檢查這行代碼:

soup.find_all('table', class_='table')

暫無
暫無

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

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