簡體   English   中英

從網頁Python抓取多個表

[英]Scraping Multiple Tables from webpage Python

我正在嘗試從下面的網頁上抓取多個表格。 但是,即使所有表都嵌套在相同的tr和td標簽中,我的代碼也只獲取第一個表。 那是我的嘗試:

 url = "http://zipnet.in/index.php?page=missing_person_search&criteria=browse_all&Page_No=1"
 r = requests.get(url)
 soup = BeautifulSoup(r.content, 'html.parser')
 tables = soup.find('table', border=1)
 for row in tables.findAll('tr'):
 sleep (3)
 col = row.findAll('td')
 fields = col[0].string
 details = col[1].string
 record = (fields, details)
 print (record)

我在這里想念什么?

試試吧,獲取該頁面中所有可用的表,尤其是包含必需記錄的表:

import requests 
from bs4 import BeautifulSoup

url = "http://zipnet.in/index.php?page=missing_person_search&criteria=browse_all&Page_No=1"
res = requests.get(url)
soup = BeautifulSoup(res.text, 'lxml')
for trow in soup.select("table#AutoNumber15"):
    data = [[' '.join(item.text.split()) for item in tcel.select("td")]
            for tcel in trow.select("tr")]
    print(data)

暫無
暫無

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

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