我正在尝试从以下网站上删除所有表格数据: 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)
答案 0 :(得分:1)
您可以在网址中设置max_rows
参数:
https://report.boonecountymo.org/mrcjava/servlet/SH01_MP.I00290s?max_rows=500