pyquery

# Python 3 & 爬虫一些记录

若如初见. 提交于 2019-11-30 10:25:27
目录 Python 3 & 爬虫一些记录 交互模式和命令行模式 函数积累 语法积累 列表和元组 输入 交互模式下输入多行 爬虫 HTTP报文请求头User-Agent信息 解析库pyquery Python 3 & 爬虫一些记录 Python任何数据都看成一个“对象”,变量指向数据对象,对变量赋值就是把数据和变量给关联起来。 Python的整数没有大小限制浮点数也没有大小限制,但是超出一定范围就直接表示为inf(无限大)。 ord()函数获取字符的整数表示,chr()函数把编码转换为对应的字符 ASCII只能表示英文和某些符号1字节,UniCode可以表示所有国家的语言2字节,ASCII前面加上一个字节的0就是Unicode,对于全英文文本使用Unicode浪费空间。 解决方案:UTF-8智能编码,英文编码成1字节,汉字编码成3字节。。。UTF-8编码把一个Unicode字符根据不同的数字大小编码成1-6个字节,常用的英文字母被编码成1个字节,汉字通常是3个字节,只有很生僻的字符才会被编码成4-6个字节。如果你要传输的文本包含大量英文字符,用UTF-8编码就能节省空间 交互模式和命令行模式 打开终端就进入了命令行模式 命令行模式下可以输入Python3 进入Python3 的交互模式,也可以 Python 1.py 运行 .py 文件 交互模式下只能输入一行代码,然后运行一行代码

python爬虫之PyQuery的基本使用

狂风中的少年 提交于 2019-11-29 20:37:10
PyQuery库也是一个非常强大又灵活的网页解析库,如果你有前端开发经验的,都应该接触过jQuery,那么PyQuery就是你非常绝佳的选择,PyQuery 是 Python 仿照 jQuery 的严格实现。语法与 jQuery 几乎完全相同,所以不用再去费心去记一些奇怪的方法了。 官网地址: http://pyquery.readthedocs.io/en/latest/ jQuery参考文档: http://jquery.cuishifeng.cn/ 1、字符串的初始化 from pyquery import PyQuery as pq html = '''<div> <ul> <li class="item-0">first item</li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-1 active"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth

Python HTML Resolution Demo - SGMLParser & PyQuery

强颜欢笑 提交于 2019-11-29 13:06:11
1. SGMLParser:   这里定义了一个Parse类,继承SGMLParser里面的方法。使用一个变量is_h4做标记判定 html文件中的h4标签,如果遇到h4标签,则将标签内的内容加入到Parse的变量name中。解释一下start_h4()和end_h4()函数,他们原型是SGMLParser中的 start_ tagname (self, attrs) end_ tagname (self) tagname就是标签名称,比如当遇到<h4>,就会调用start_h4,遇到</h4>,就会调用 end_h4。attrs为标签的参数,以[(attribute, value), (attribute, value), ...]的形式传回。 Demo: #!/usr/bin/python2.7 # FileName: sgmlparser.py # Author: lxw # Date: 2015-07-30 import urllib2 from sgmllib import SGMLParser class Parse(SGMLParser): def __init__(self): SGMLParser.__init__(self) self.is_h4 = "" self.name = [] self.is_a = "" self.link = [] def

pyquery的使用

我与影子孤独终老i 提交于 2019-11-29 12:26:31
from pyquery import pyQuery as pq url初始化 doc = pq(url=" ") 文件初始化 doc = qp(filename="路径") 基本css选择器 参考链接: https://www.cnblogs.com/lei0213/p/7676254.html 来源: CSDN 作者: 初学者中的小白 链接: https://blog.csdn.net/qq_41671718/article/details/103229490

pip error: unrecognized command line option ‘-fstack-protector-strong’

寵の児 提交于 2019-11-29 09:21:12
When I sudo pip install pyquery , sudo pip install lxml , and sudo pip install cython , I get very similar output with the same error that says: x86_64-linux-gnu-gcc: error: unrecognized command line option ‘-fstack-protector-strong’ Here's the complete pip output for sudo pip install pyquery : Requirement already satisfied (use --upgrade to upgrade): pyquery in /usr/local/lib/python2.7/dist-packages Downloading/unpacking lxml>=2.1 (from pyquery) Running setup.py egg_info for package lxml /usr/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'bugtrack_url'

Python pyquery

雨燕双飞 提交于 2019-11-28 22:08:45
pyquery 一个像 jQuery 一样的解析库 初始化 字符串初始化 from pyquery import PyQuery as pq html = '' doc = pq(html) 文件初始化 from pyquery import PyQuery as pq doc = pq(filename='') URL 初始化 from pyquery import PyQuery as pq doc = pq(url='https://cnblogs.com/dbf-') 选择器 from pyquery import PyQuery as pq html = '' doc = pq(html) doc('#i1') # id 选择器 doc('.c1') # class 选择器 doc('div') # 标签选择器 doc('#i1, #i2') # 组合选择器 id==i1 或 id==i2 doc('#i1.c1') # 组合选择器 id==i1 且 id==i2 doc('#i1 .c1') # 层级选择器 id==i1 下所有 class==c1 的标签 doc('div > .c1') # 层级选择器 id==i1 下一层 class==c1 的标签 伪类选择器 from pyquery import PyQuery as pq html = '' doc = pq

爬虫之PyQuery的base了解

不羁岁月 提交于 2019-11-28 18:32:15
爬虫之PyQuery的base了解 pyquery库是jQuery的Python实现,能够以jQuery的语法来操作解析 HTML 文档,易用性和解析速度都很好,和它差不多的还有BeautifulSoup,都是用来解析的。相比BeautifulSoup完美翔实的文档,虽然PyQuery库的文档弱爆了, 但是使用起来还是可以的,有些地方用起来很方便简洁。 本地文件test.html <html lang="en"> <head> <meta charset="UTF-8" /> <title>测试bs4</title> </head> <body> <div> <p>百里守约</p> </div> <div class="song"> <p>李清照</p> <p>王安石</p> <p>苏轼</p> <p>柳宗元</p> <a href="http://www.song.com/" title="赵匡胤" target="_self"> <span>this is span</span> 宋朝是最强大的王朝,不是军队的强大,而是经济很强大,国民都很有钱</a> <a href="" class="du">总为浮云能蔽日,长安不见使人愁</a> <img src="http://www.baidu.com/meinv.jpg" alt="" /> </div> <div class=

pyquery:轻松、灵活的处理html

我与影子孤独终老i 提交于 2019-11-28 05:51:05
介绍 pyquery是一个专门用来解析html的库,从名字很容易想到jQuery,没错,这完全是仿照jQuery的语法实现的。如果用过jQuery,俺么pyquery很容易实现 初始化html pyquery可以接收一个网址,自动下载内容,也可以接收已经下载好的字符串格式的html,当然也可以传入一个本地html文件。但是我们一般都会使用requests下载html页面,然后再将html页面以字符串的格式传进去 python from pyquery import PyQuery html = ''' <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>古明地觉</title> </head> <body> <p id="bili"><a href="http://www.bilibili.com">想进入基佬的大门吗?还等什么,快点击吧</a></p> <p class="s1">my name is satori</p> <div> <p class="s1">古明地恋</p> </div> <table > <tbody> <tr> <td>姓名:</td> <td><input type="text" name="name"></td> </tr> <tr class="tr"> <td

pip error: unrecognized command line option ‘-fstack-protector-strong’

戏子无情 提交于 2019-11-28 02:47:33
问题 When I sudo pip install pyquery , sudo pip install lxml , and sudo pip install cython , I get very similar output with the same error that says: x86_64-linux-gnu-gcc: error: unrecognized command line option ‘-fstack-protector-strong’ Here's the complete pip output for sudo pip install pyquery : Requirement already satisfied (use --upgrade to upgrade): pyquery in /usr/local/lib/python2.7/dist-packages Downloading/unpacking lxml>=2.1 (from pyquery) Running setup.py egg_info for package lxml

pyquery 库的使用

与世无争的帅哥 提交于 2019-11-27 21:39:00
1 from pyquery import PyQuery as pq 2 # 文件勿命名为 pyquery.py,会发生冲突 3 4 # 字符串初始化 5 html = ''' 6 <div id="page"> 7 <div id="car_test"> 8 <ul class="menu-list"> 9 <li class="icon1"><a href="link1.html">科目一</a></li> 10 <li class="icon2"><a href="link2.html">科目二</a></li> 11 <li class="icon3 subject"><a href="link3.html">科目三</a></li> 12 <li class="icon4"><a href="link4.html">科目四</a></li> 13 <li class="buy car"><a href="link4.html">买车</a></li> 14 </ul> 15 </div> 16 </div> 17 ''' 18 doc = pq(html) 19 print(doc('li')) 20 # url初始化 21 doc = pq(url='https://www.jiakaobaodian.com/') 22 print(doc('title')) 23