从网页Python中刮取多个表格

时间:2018-01-01 16:35:14

标签: python web-scraping beautifulsoup python-requests

我正在尝试从下面的网页上抓取多个表格。但是,我的代码只获得第一个表,即使所有表都嵌套在相同的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)

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

试一试,获取该页面中的所有表格,尤其是包含所需记录的表格:

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)