python BeautifulSoup 获取页面多个子节点中的各个节点的内容

时间:2022-01-24 22:58:36

页面html格式为

<tr bgcolor="#7bb5de">
<td style="border-bottom: 1px solid #C9D8AD" width="118" align="center" bgcolor="#D9E6FF">
<p align="center">
lyl5577d92</p></td>
<td style="border-bottom: 1px solid #C9D8AD" width="96" align="center" bgcolor="#D9E6FF">
<p align="center">李永利</p></td>
<td align="center" style="border-bottom: 1px solid #C9D8AD" width="105" bgcolor="#D9E6FF">
<div align="center"><font color="#FF0000">lyl5577d</font></div>
</td>
<td align="center" style="border-bottom: 1px solid #C9D8AD" width="153" bgcolor="#D9E6FF">
<div align="center">469680008</div>
</td>
<td align="center" style="border-bottom: 1px solid #C9D8AD" width="218" bgcolor="#D9E6FF">
<div align="center">2016-05-21 15:24:27.0</div>
</td>
<td align="center" style="border-bottom: 1px solid #C9D8AD" width="171" bgcolor="#D9E6FF">
<div align="center">0</div>
</td>
<td align="center" style="border-bottom: 1px solid #C9D8AD" width="119" bgcolor="#D9E6FF">0</td>
<td align="center" style="border-bottom: 1px solid #C9D8AD" width="111" bgcolor="#D9E6FF">0</td>
<td align="center" style="border-bottom: 1px solid #C9D8AD" width="87" bgcolor="#D9E6FF">0</td>
<td align="center" style="border-bottom: 1px solid #C9D8AD" width="128" bgcolor="#D9E6FF">0</td>
</tr>

 import httplib
from BeautifulSoup import BeautifulSoup def main():
f = open('result','a') headers = {'Content-Type':'application/x-www-form-urlencoded',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding': 'gzip, deflate',
'Referer': 'http://xxx.xxx.com/admin/userlist',
'Cookie': 'JSESSIONID=9F6F2D03D2C11400B3D6731E90D73117',
'User-Agent': 'User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:46.0) Gecko/20100101 Firefox/46.0',
} conn = httplib.HTTPConnection('*.*.*.*', timeout=50) for p in range(1,1287):
print p
conn.request(method='GET',
url="/admin/userlist?toPage=%s&sessionID=" % str(p),
headers=headers)
resp = conn.getresponse()
html_doc = resp.read()
mainSoup = BeautifulSoup(html_doc)
for s in mainSoup.findAll('tr', attrs={'bgcolor':'#7bb5de'}):
if 'style' not in str(s):
continue
for d in s.findAll('td'):
print d.getText(),
f.write("%s " % d.getText().encode('utf-8')) #f.write("%s " % d.getText())==> UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
f.write("%s\n" % d.getText().encode('utf-8'))
print
f.close()
conn.close() if __name__ == '__main__':
main()