<tr id="inmate_201700220865">
<td class="row ">3</td>
<td class="row "><a href="javascript:" onclick="getInmatePreview(201700220865)">View</a>
<input type="hidden" id="bookingPhoto_201700220865" value="http://bookings.example.org/201708/20170826.AA8">
<input type="hidden" id="bookingPhotoFile_201700220865" value="20170826.AA8">
<input type="hidden" id="bookingPhotoFolder_201700220865" value="201708">
<input type="hidden" id="bookingPhotoName_201700220865" value="LAST, FIRST LAST">
<input type="hidden" id="inmateID_201700220865" value="277497">
<input type="hidden" id="index_2" value="201700220865">
<input type="hidden" id="curIndex_201700220865" value="2"></td>
<td class="row ">LAST<input type="hidden" id="bookingLastName_201700220865" value="LAST"></td>
<td class="row ">FIRST<input type="hidden" id="bookingFirstName_201700220865" value="FIRST"></td>
<td class="row ">LAST<input type="hidden" id="bookingLastName_201700220865" value="LAST"></td>
<td class="row ">08/26/2017</td>
<td class="row ">41</td>
<td class="row ">M</td>
</tr>
I am attempting to scrape the last 6 lines of text from this table. I am having difficulty without performing nested loops through Beautiful Soup. I'm sure there is an easier way, but for the record I just need the Last name, First name, Last name, and the last three lines which are DOB, Age and Gender. Below is my code which returns the entire tr.
from bs4 import BeautifulSoup
from urllib.request import urlopen
import re
#beautiful soup scrape
scraped = urlopen('http://www.example.org/inmates/').read()
soup = BeautifulSoup(scraped, 'html.parser')
for item in soup.find_all('tr',{'id' : re.compile('^inmate') }):
for name in item ('td',{'class' : "row alt"}):
print (item)
Thanks in advance
Find all the tr tags and get the texts using get_text() method. Then split() the text by \n and remove the empty strings using filter. Here you get all the data you need in one line.
from bs4 import BeautifulSoup
from urllib.request import urlopen
import re
#beautiful soup scrape
scraped = urlopen('http://www.example.org/inmates/').read()
soup = BeautifulSoup(scraped, 'html.parser')
for item in soup.find_all('tr', {'id' : re.compile('^inmate')}):
data = list(filter(None, item.get_text().split('\n')))
print(data)
Output
['3', 'View', 'LAST Name', 'FIRST Name', 'LAST Name', '08/26/2017', '41', 'M']
If you want to remove the first 2 elements then just slice the list
data = list(filter(None, item.get_text().split('\n')))[2:]
Output
['LAST', 'FIRST', 'LAST', '08/26/2017', '41', 'M']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With