美丽汤用蟒蛇刮表ID



我是刮擦新手,正在学习使用美丽汤,但我在刮桌子时遇到了麻烦。对于我正在尝试解析的 HTML:

<table id="ctl00_mainContent_DataList1" cellspacing="0" > style="width:80%;border-collapse:collapse;"> == $0
<tbody>
<tr><td><table width="90%" cellpadding="5" cellspacing="0">...</table></td></tr>
<tr><td><table width="90%" cellpadding="5" cellspacing="0">...</table></td></tr>
<tr><td><table width="90%" cellpadding="5" cellspacing="0">...</table></td></tr>
<tr><td><table width="90%" cellpadding="5" cellspacing="0">...</table></td></tr>
...

我的代码:

from urllib.request import urlopen
from bs4 import BeautifulSoup
quote_page = 'https://www.bcdental.org/yourdentalhealth/findadentist.aspx'
page = urlopen(quote_page)
soup = BeautifulSoup(page, 'html.parser')
table = soup.find('table', id="ctl00_mainContent_DataList1")
rows = table.findAll('tr')

我得到AttributeError: 'NoneType' object has no attribute 'findAll'.我正在使用python 3.6和jupyter笔记本,以防万一。

编辑: 我尝试解析的表数据仅在请求搜索后显示在页面上(在city字段中,选择Burnaby,然后点击搜索)。ctl00_mainContent_DataList1表是提交搜索后显示的牙医列表。

首先:我使用requests,因为它更容易使用cookie,标头等。


页面由ASP.net生成,它__VIEWSTATE__VIEWSTATEGENERATOR__EVENTVALIDATION发送值,您也必须POST请求中发送这些值。

您必须使用GET加载页面,然后才能获取这些值。
您还可以使用request.Session()来获取也需要的cookie。

接下来,您必须从表单中复制值并添加参数并使用POST发送。

在代码中,我只放置始终发送的参数。

'526'Vancouver的代码。您可以在<select>标签中找到其他代码。
如果需要其他选项,则可能需要添加其他参数。

即。ctl00$mainContent$chkUndr4Ref: on是为Children: 3 & Under - Diagnose & Refer

编辑:因为内部<tr><table>所以find_all('tr')返回太多元素(外部tr和内部tr)和and laterfind_all('td')give the sametdmany times. I changedfind_all('tr')intofind_all('table')',它应该停止重复数据。

import requests
from bs4 import BeautifulSoup
url = 'https://www.bcdental.org/yourdentalhealth/findadentist.aspx'
# --- session ---
s = requests.Session() # to automatically copy cookies
#s.headers.update({'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0'})
# --- GET request ---
# get page to get cookies and params
response = s.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# --- set params ---
params = {
# session - copy from GET request
#'EktronClientManager': '',
#'__VIEWSTATE': '',
#'__VIEWSTATEGENERATOR': '',
#'__EVENTVALIDATION': '',
# main options
'ctl00$terms': '',
'ctl00$mainContent$drpCity': '526',
'ctl00$mainContent$txtPostalCode': '',
'ctl00$mainContent$drpSpecialty': 'GP',
'ctl00$mainContent$drpLanguage': '0',
'ctl00$mainContent$drpSedation': '0',
'ctl00$mainContent$btnSearch': '+Search+',
# other options
#'ctl00$mainContent$chkUndr4Ref': 'on',
}
# copy from GET request
for key in ['EktronClientManager', '__VIEWSTATE', '__VIEWSTATEGENERATOR', '__EVENTVALIDATION']:
value = soup.find('input', id=key)['value']
params[key] = value
#print(key, ':', value)
# --- POST request ---
# get page with table - using params
response = s.post(url, data=params)#, headers={'Referer': url})
soup = BeautifulSoup(response.text, 'html.parser')
# --- data ---
table = soup.find('table', id='ctl00_mainContent_DataList1')
if not table:
print('no table')
#table = soup.find_all('table')
#print('count:', len(table))
#print(response.text)
else:   
for row in table.find_all('table'):
for column in row.find_all('td'):
text = ', '.join(x.strip() for x in column.text.split('n') if x.strip()).strip()
print(text)
print('-----')

部分结果:

Map
Dr. Kashyap Vora, 6145 Fraser Street, Vancouver  V5W 2Z9
604 321 1869, www.voradental.ca
-----
Map
Dr. Niloufar Shirzad, Harbour Centre DentalL19 - 555 Hastings Street West, Vancouver  V6B 4N6
604 669 1195, www.harbourcentredental.com
-----
Map
Dr. Janice Brennan, 902 - 805 Broadway West, Vancouver  V5Z 1K1
604 872 2525
-----
Map
Dr. Rosemary Chang, 1240 Kingsway, Vancouver  V5V 3E1
604 873 1211
-----
Map
Dr. Mersedeh Shahabaldine, 3641 Broadway West, Vancouver  V6R 2B8
604 734 2114, www.westkitsdental.com
-----

最新更新