Eric
Eric

Reputation: 23

Using BeautifulSoup To Scrape Data

I am trying to parse an html with BeautifulSoup but unable to get the data

<tr>
<td align="left" colspan="3" style="font-size:10.0pt;font-weight:800;" valign="top">49-009-41057
<td align="left" colspan="4" style="font-size:10.0pt;font-weight:800;" valign="top">CHESAPEAKE OPERATING LLC 
<td align="left" colspan="1" style="font-size:10.0pt;font-weight:800;" valign="top"> 
<tr>
<td align="left" colspan="3" style="border-top:none; border-left:none;border-bottom:none; border-right:none;padding:.01in .01in .01in .01in;height:6.75pt" valign="top"><span style="font-size:5.75pt;font-family:Arial;color:Darkgray;">Well Name</span></td>
<td align="left" colspan="4" style="border-top:none; border-left:none;border-bottom:none; border-right:none;padding:.01in .01in .01in .01in;height:6.75pt" valign="top"><span style="font-size:5.75pt;font-family:Arial;color:Darkgray;">Field</span></td>
<td align="left" colspan="1" style="border-top:none; border-left:none;border-bottom:none; border-right:none;padding:.01in .01in .01in .01in;height:6.75pt" valign="top"><span style="font-size:5.75pt;font-family:Arial;color:Darkgray;"> </span></td>
<tr>
<td align="left" colspan="3" style="font-size:10.0pt;font-weight:800;" valign="top">SFU 10-34-72 USAC TR 23H 
<td align="left" colspan="4" style="font-size:10.0pt;font-weight:800;" valign="top">WC 
<td align="left" colspan="1" style="font-size:10.0pt;font-weight:800;" valign="top">
<tr>
<td align="left" colspan="3" style="border-top:none; border-left:none;border-bottom:none; border-right:none;padding:.01in .01in .01in .01in;height:6.75pt" valign="top"><span style="font-size:5.75pt;font-family:Arial;color:Darkgray;">Surface Location</span></td>
<td align="left" colspan="1" style="border-top:none; border-left:none;border-bottom:none; border-right:none;padding:.01in .01in .01in .01in;height:6.75pt" valign="top"><span style="font-size:5.75pt;font-family:Arial;color:Darkgray;">Section</span></td>
<td align="left" colspan="2" style="border-top:none; border-left:none;border-bottom:none; border-right:none;padding:.01in .01in .01in .01in;height:6.75pt" valign="top"><span style="font-size:5.75pt;font-family:Arial;color:Darkgray;">Township/Range</span></td>
<td align="left" colspan="1" style="border-top:none; border-left:none;border-bottom:none; border-right:none;padding:.01in .01in .01in .01in;height:6.75pt" valign="top"><span style="font-size:5.75pt;font-family:Arial;color:Darkgray;">Latitude</span></td>
<td align="left" colspan="1" style="border-top:none; border-left:none;border-bottom:none; border-right:none;padding:.01in .01in .01in .01in;height:6.75pt" valign="top"><span style="font-size:5.75pt;font-family:Arial;color:Darkgray;">Longitude</span></td>
<tr>
<td align="left" colspan="3" style="font-size:10.0pt;font-weight:800;" valign="top">2188 FNL AND 984 FEL  ( SE NE )
<td align="left" colspan="1" style="font-size:10.0pt;font-weight:800;" valign="top">10 
<td align="left" colspan="2" style="font-size:10.0pt;font-weight:800;" valign="top">34 NORTH 72 WEST
<td align="left" colspan="1" style="font-size:10.0pt;font-weight:800;" valign="top">42.934003 
<td align="left" colspan="1" nowrap="" style="font-size:10.0pt;font-weight:800;" valign="top">-105.480115 

I was able to use BeautifulSoup to get the HTML

soup = BeautifulSoup(body, 'html.parser')
tr = soup.find_all('tr')

How can I get values API = 49-009-41057, Company = CHESAPEAKE OPERATING LL, Well Name = SFU 10-34-72 USAC TR 23H, etc?

Upvotes: 1

Views: 50

Answers (1)

Sureshmani Kalirajan
Sureshmani Kalirajan

Reputation: 1938

BS Approach,

soup = BeautifulSoup(body, 'html.parser')
trs = soup.find_all('tr')

for tr in trs:
    tds = tr.find_all('td')
    for td in tds:
        print(td.text)

or You can try with lxml and xpaths,

from lxml import html


html = html.parse(body)

td1 = html.xpath("//tr/td[1]")
for td in td1:
    print(td.text)
# repeat the same for other tds in the html body.

Output: BS approach should print in sequential order. the lxml approach should print something like this,

'49-009-41057'

'SFU 10-34-72 USAC TR 23H '

'2188 FNL AND 984 FEL ( SE NE )

Upvotes: 1

Related Questions