2017-03-17 69 views
-1

如何颳去HTML表中的數據在python

<tr class="even"> 
 
<td><strong><a href='../eagleweb/viewDoc.jsp?node=DOC186S8881'>DEED<br/> 
 
2016002023</a></strong></td> 
 
<td><a href='../eagleweb/viewDoc.jsp?node=DOC186S8881'><b> Recording Date: </b>01/12/2016 08:05:17 AM&nbsp;&nbsp;&nbsp;<b>Book Page: </b> <table cellspacing=0 width="100%"><tr><td width="50%" valign="top"><b>Grantor:</b> ARELLANO ISAIAS</td><td width="50%" valign="top"><b>Grantee:</b> ARELLANO ISAIAS, ARELLANO ALICIA</td></tr></table> 
 
<b>Number Pages:</b> 3<br></a></td> 
 
<td></td> 
 
<td></td></tr>

我是新來的Python和拼搶請幫助我如何從該表中抽取數據。 要登錄,請轉到公共登錄頁面,然後輸入日期和日期。數據模型:數據模型具有以下特定順序的列:「record_date」,「doc_number」,「doc_type」,「role」,「name」,「apn」,「transfer_amount」,「county」 ,和「國家」。根據姓名的分配位置,「角色」列可以是「授予者」或「受讓人」。如果授權人和受讓人有多個姓名,請給每個姓名一個新行,並複製記錄日期,文檔編號,文檔類型,角色和apn。

https://crarecords.sonomacounty.ca.gov/recorder/eagleweb/docSearchResults.jsp?searchId=0

+0

我想提取這些東西。數據模型:數據模型具有以下特定順序的列:「record_date」,「doc_number」,「doc_type」,「role」,「name」,「apn」,「transfer_amount」,「county」和「state 」。根據姓名的分配位置,「角色」列可以是「授予者」或「受讓人」。如果授權人和受讓人有多個姓名,請給每個姓名一個新行,並複製記錄日期,文檔編號,文檔類型,角色和apn。如果您對如何構建csv結果有疑問,請詢問我。 –

+0

這看起來像一個安全的網站需要憑據,我只能'你必須登錄訪問請求的頁面。你能否將html表格複製到你的問題中? – davedwards

+0

好的等待我會把截圖 –

回答

0

您發佈不包含所有數據模型中列出的列字段的HTML。然而,它包含的字段,這將產生一個Python dictionary,你可以得到的數據模型中的字段:

import urllib.request 
from bs4 import BeautifulSoup 

url = "the_url_of_webpage_to_scrape" # Replace with the URL of your webpage 

with urllib.request.urlopen(url) as response: 
    html = response.read() 

soup = BeautifulSoup(html, 'html.parser') 

table = soup.find("tr", attrs={"class":"even"}) 

btags = [str(b.text).strip().strip(':') for b in table.find_all("b")] 

bsibs = [str(b.next_sibling.replace(u'\xa0', '')).strip() for b in table.find_all('b')] 

data = dict(zip(btags, bsibs)) 

data_model = {"record_date": None, "doc_number": None, "doc_type": None, "role": None, "name": None, "apn": None, "transfer_amount": None, "county": None, "state": None} 

data_model["record_date"] = data['Recording Date'] 
data_model['role'] = data['Grantee'] 

print(data_model) 

輸出:

{'apn': None, 
'county': None, 
'doc_number': None, 
'doc_type': None, 
'name': None, 
'record_date': '01/12/2016 08:05:17 AM', 
'role': 'ARELLANO ISAIAS, ARELLANO ALICIA', 
'state': None, 
'transfer_amount': None} 

有了這個,你可以這樣做:

print(data_model['record_date']) # 01/12/2016 08:05:17 AM 
print(data_model['role'])  # ARELLANO ISAIAS, ARELLANO ALICIA 

希望這會有所幫助。

+0

謝謝@downshift :) –