python BeautifulSoup 获取页面多个子节点中的各个节点的内容
页面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'))
f.close()
conn.close() if __name__ == '__main__':
main()
python BeautifulSoup 获取页面多个子节点中的各个节点的内容的更多相关文章
- zTree实现单独选中根节点中第一个节点
zTree实现单独选中根节点中第一个节点 1.实现源码 <!DOCTYPE html> <html> <head> <title>zTree实现基本树& ...
- python beautifulsoup获取特定html源码
beautifulsoup 获取特定html源码(无需登录页面) import refrom bs4 import BeautifulSoupimport urllib2 url = 'http:// ...
- python中用ElementTree.iterparse()读取xml文件中的多层节点
我在使用Python解析比较大型的xml文件时,为了提高效率,决定使用iterparse()方法,但是发现根据网上的例子:每次if event == 'end':之后elem.clear()或者是每次 ...
- python实现剑指offer删除链表中重复的节点
题目描述 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3->3->4->4->5 处理后 ...
- python之获取页面标签的方法
from urllib.request import urlopen from urllib.error import HTTPError from bs4 import BeautifulSoup ...
- python 自动获取(打印)代码中的变量的名字字串
方法一: import inspectimport re def varname(p): for line in inspect.getframeinfo(inspect.currentframe() ...
- JS(基础)_总结获取页面中元素和节点的方式
一.前言 1.元素和节点的区别 2.总结获取元素的方式 3.总结获取节点的方式 二.主要内容 1.结点和元素的区别 (1)一些常见基本概念: 文档:document 元素:页面中所有的标签 结点:页面 ...
- python爬虫主要就是五个模块:爬虫启动入口模块,URL管理器存放已经爬虫的URL和待爬虫URL列表,html下载器,html解析器,html输出器 同时可以掌握到urllib2的使用、bs4(BeautifulSoup)页面解析器、re正则表达式、urlparse、python基础知识回顾(set集合操作)等相关内容。
本次python爬虫百步百科,里面详细分析了爬虫的步骤,对每一步代码都有详细的注释说明,可通过本案例掌握python爬虫的特点: 1.爬虫调度入口(crawler_main.py) # coding: ...
- webAPI(DOM) 2.1 获取页面元素 | 事件1 | 属性操作 | 节点 | 创建元素 | 事件2
js分三个部分: ECMAScript标准:js的基本语法 DOM:Ducument Object Model--->文档对象模型--->操作页面的元素 BOM:Browser Objec ...
随机推荐
- hdoj--1260--Tickets(简单dp)
Tickets Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- POJ 3693 后缀数组+RMQ
思路: 论文题 后缀数组&RMQ 有一些题解写得很繁 //By SiriusRen #include <cmath> #include <cstdio> #includ ...
- mybatis批量插入、批量更新和批量删除
转载 https://www.jianshu.com/p/041bec8ae6d3
- LeetCode(10)Regular Expression Matching
题目如下: Python代码: # -*- coding:utf-8 -*- def ismatch(s,p): #先将dp[s+1][p+1]二维数组全置为False dp = [[False] * ...
- ZBrush中Zproject与SubTool的综合应用
在ZBrush中有很多工具也需要配合子物体工具来使用,如笔刷中的Zproject(投射笔刷)就需要子物体工具来配合使用,本文将讲解一下它的用法. 1. 首先创建一个平面,在Tool(工具)面板中选择P ...
- Vim常用命令及配置方案
Vim常用命令及配置方案 几句话 很久之前就接触到vim,初学那阵觉得vim很酷炫,但确实对新手不是很友好.我也就简单看了下基本操作就上手了,但又不是长期在vim下工作,这就导致了每一次重新使用v ...
- matlab Time-domain analysis 渐进式或者实时获取仿真值
首先准备一个传递函数sys, 然后使用lsim(sys,u,t,x0)函数(通用的时序分析的函数) u: The input u is an array having as many rows as ...
- ansible搭建mysql主主模式
ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)等优点,实现了批量系统配置.批量程序部署.批量运行命 ...
- Python 中多线程之 _thread
_thread模块是python 中多线程操作的一种模块方式,主要的原理是派生出多线程,然后给线程加锁,当线程结束的 时候取消锁,然后执行主程序 thread 模块和锁对象的说明 start_new_ ...
- [luogu] P4514 上帝造题的七分钟 (树状数组,二维差分)
P4514 上帝造题的七分钟 题目背景 裸体就意味着身体. 题目描述 "第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a ...