2016-11-28 92 views
0

我试图刮掉以下站点中的所有表数据: https://report.boonecountymo.org/mrcjava/servlet/SH01_MP.I00290s获取所有表行,不默认情况下使用BeautifulSoup

表共有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

谢谢@jinksPadlock!这工作完美。我很欣赏快速反应。 –

+0

如果没有办法设置表中要查看的最大行数,是否有任何方法可以为第一页,第二页,然后是第三页等提取结果? –

+0

由于表格从设置输入值重新加载,您的脚本将需要处理JavaScript。像Selenium这样的东西可以做到这一点。 – jinksPadlock

相关问题