PyQuery基本操作介绍
2016-01-31 20:13
603 查看
PyQuery基本操作介绍
PyQuery为Python提供一个类似于jQuery对HTML的操作方式,可以使用jQuery的语法对html文档进行查询操作。 本文以百度首页为例来介绍PyQuery的一些基本操作。
初始化pyquery
from pyquery import PyQuery as pq doc = pq(url='http://www.baidu.com') print(type(doc))
<class 'pyquery.pyquery.PyQuery'>
# 获取导航链接的父元素(id='u1') products = doc('#u1') print(type(products))
<class 'pyquery.pyquery.PyQuery'>
link_index_first = products('a:first') link_index_last = products('a:last') link_index_custom = products('a:eq(2)') print(type(link_index_first))
<class 'pyquery.pyquery.PyQuery'>
可以通过PyQuery的text()方法来获取其对应的文字
print(link_index_first.text()) print(link_index_last.text()) print(link_index_custom.text())
糯米
更多产品
hao123
也可以通过PyQuery的attr()方法来获取元素的属性
print(link_index_first.attr('name'))
tj_trnuomi
下面来遍历所有导航按钮。 P.S. 注意此时link的类型是“lxml.html.HtmlElement”
# 遍历所有导航链接,并显示链接的name属性和在网页上显示的文字 links = products('a') for link in links: id_name = link.get('name') text = link.text print('Name: {0: <15}\tText: {1: <15}'.format(id_name, text))
Name: tj_trnuomi Text: 糯米
Name: tj_trnews Text: 新闻
Name: tj_trhao123 Text: hao123
Name: tj_trmap Text: 地图
Name: tj_trvideo Text: 视频
Name: tj_trtieba Text: 贴吧
Name: tj_login Text: 登录
Name: tj_settingicon Text: 设置
Name: tj_briicon Text: 更多产品
下面介绍一下初始化PyQuery时的另外两种参数
直接转换字符串d = pq("<html></html>") d = pq(etree.fromstring("<html></html>"))
读取文件
d = pq(filename=path_to_html_file)
另外,在处理需要编码的文件时可以使用如下的方法:
from lxml.html import HTMLParser, fromstring UTF8_PARSER = HTMLParser(encoding='utf-8') with open(page, encoding='utf-8') as filehandler: file_contents = filehandler.read() doc = pq(fromstring(file_contents, parser = UTF8_PARSER))
相关文章推荐
- 和为0的4个值(4 Values Whose sum is Zero UVa 1152)
- NGUI ScrollView 最彻底优化方案 加载大量(百万级)Item不卡的()
- Bluez调试工具hcitool与gattool的使用实例
- iOS使用XZMRefresh实现UITableView或UICollectionView横向刷新
- B. Guess the Permutation
- UINavigationController练习
- 修改Request 中的数据
- RabbitMQ学习笔记
- Intergraph CADWorx (Plant, P&ID, Equipment, IP, SpecEditor) 2016 v16.0-ISO 1DVD
- java.sql.SQLException: General error, message from server: "Field 'ID' doesn't have a default value
- java.net.SocketException: Can't assign requested address
- busybox filesystem matrix-gui-2.0 undefined function json_encode()
- UItextView回收键盘的几种方式
- LeetCode Unique Binary Search Trees
- android-Building a Notification,Preserving Navigation when Starting an Activity
- Codeforces 67C Sequence of Balls 编辑距离 dp
- BZOJ-1206 虚拟内存 Hash+离散化+Priority_Queue
- BZOJ-1206 虚拟内存 Hash+离散化+Priority_Queue
- iOS UITextField
- iOS8下的UIAlertContoller初探