使用BeautifulSoup获取所有表行,而不是默认值

时间:2016-11-28 13:07:17

标签: python-3.x web-scraping beautifulsoup html-table

我正在尝试从以下网站上删除所有表格数据: https://report.boonecountymo.org/mrcjava/servlet/SH01_MP.I00290s

该表总共有230行(不包括标题行),但默认为前50行。当我单击表格上的下一页按钮(箭头)时,会加载一个或多个新行,但网页不会更改。如何使用BeautifulSoup获取所有230行而不是默认的50行?

这是我正在使用的代码:

import csv
import requests
from bs4 import BeautifulSoup

url = "http://www.showmeboone.com/sheriff/JailResidents/JailResidents.asp"
response = requests.get(url)
html = response.content

soup = BeautifulSoup(html,"html.parser")
table = soup.find('tbody', attrs={'class':'stripe'})

list_of_rows = []
for row in table.findAll('tr'):
    list_of_cells = []
    for cell in row.findAll('td'):
        text = cell.text.replace(' ', '')
        list_of_cells.append(text)
    list_of_rows.append(list_of_cells[1:])

outfile = open("./inmates.csv", "w", newline='')
writer = csv.writer(outfile)
writer.writerow(["Last", "First", "Middle", "Gender", "Race", "Age", "City", "State"])
writer.writerows(list_of_rows)

1 个答案:

答案 0 :(得分:1)