1.pyquery
1.1 介绍
1.2 安装
pip install pyquery
1.3 使用
1.3.1 初始化
- 字符串
from pyquery import PyQuery as pq
doc=pq(str)
print(doc(tagname))
- url
from pyquery import PyQuery as pq
doc=pq(url)
print(doc(‘title’))
- 文件
from pyquery import PyQuery as pq
doc=pq(filename)
print(doc(tagname))
1.3.2 选择节点
- 选取当前节点
doc(’#main #top’)
- 选取子节点
- 在doc中一层层写出来
- 获取到父标签后使用children方法
doc(’#main #top’).children()
- 获取父节点
- 获取到当前节点后使用parent方法
- 获取兄弟结点
- 获取当前节点后使用sliblings方法
1.3.3 获取属性
doc(’#main #top’).attrib[‘href’]
1.3.4 获取内容
doc(’#main #top’).html()
doc(’#main #top’).text()
1.3.5小例子
from pyquery import PyQuery as pq
from random import choice
import requests
user_agents=[
"User-Agent:Mozilla/5.0(Windows;U;WindowsNT6.1;en-us)AppleWebKit/534.50(KHTML,likeGecko)Version/5.1Safari/534.50",
"User-Agent:Mozilla/5.0(WindowsNT6.1;rv:2.0.1)Gecko/20100101Firefox/4.0.1",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
]
headers={
"User-Agent":choice(user_agents)
}
url="http://www.xicidaili.com/nn/"
response=requests.get(url,headers)
doc=pq(response.text)
trs=doc('#ip_list tr')
for num in range(1,len(trs)):
ip =trs.eq(num).find('td').eq(1).text()
port = trs.eq(num).find('td').eq(1).text()
type = trs.eq(num).find('td').eq(1).text()
print(ip,":",port,"-----",type)