陈艾熙个人资料,东田造型学校,lg手机广告歌曲
http协议中,最常用的就是get方法: import requests response = requests.get('http://www.baidu.com') print(response.status_code) # 打印状态码 print(response.url) # 打印请求url print(response.headers) # 打印头信息 print(response.cookies) # 打印cookie信息 print(response.text) #以文本形式打印网页源码 print(response.content) #以字节流形式打印
import requests requests.get('http://httpbin.org/get') requests.post('http://httpbin.org/post') requests.put('http://httpbin.org/put') requests.delete('http://httpbin.org/delete') requests.head('http://httpbin.org/get') requests.options('http://httpbin.org/get')
经过beautiful库解析后得到的soup文档按照标准缩进格式的结构输出,为结构化的数据,为数据过滤提取做出准备。
soup.find_all('div',"item") #查找div标签,class="item"
find_all(name, attrs, recursive, string, limit, **kwargs) @params: name: 查找的value,可以是string,list,function,真值或者re正则表达式 attrs: 查找的value的一些属性,class等。 recursive: 是否递归查找子类,bool类型 string: 使用此参数,查找结果为string类型;如果和name搭配,就是查找符合name的包含string的结果。 limit: 查找的value的个数 **kwargs: 其他一些参数
find()方法与find_all()方法类似,只是find_all()方法返回的是文档中符合条件的所有tag,是一个集合,find()方法返回的一个tag
soup.selector(div.item > a > h1) 从大到小,提取需要的信息,可以通过浏览器复制得到。
示例:
<html><head><title>the dormouse's story</title></head> <body> <p class="title" name="dromouse"><b>the dormouse's story</b></p> <p class="story">once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link1"><!-- elsie --></a>, <a href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link2">lacie</a> and <a href="http://example.com/tillie" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link3">tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """
在写css时,标签名不加任何修饰,类名前加点,id名前加 #,我们可以用类似的方法来筛选元素,用到的方法是soup.select(),返回类型是list。
(1).通过标签名查找
print(soup.select('title')) #筛选所有为title的标签,并打印其标签属性和内容 # [<title>the dormouse's story</title>] print(soup.select('a')) #筛选所有为a的标签 # [<a class="sister" href="http://example.com/elsie" id="link1"><!-- elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">tillie</a>] print(soup.select('b')) #筛选所有为b的标签,并打印 # [<b>the dormouse's story</b>]
(2).通过类名查找
print soup.select('.sister') #查找所有class为sister的标签,并打印其标签属性和内容 # [<a class="sister" href="http://example.com/elsie" id="link1"><!-- elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">tillie</a>]
(3).通过id名查找
print soup.select('#link1') #查找所有id为link1的标签,并打印其标签属性和内容 #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- elsie --></a>]
(4).组合查找
组合查找即和写class文件时,标签名与类名、id名进行的组合原理是一样的,例如查找p标签中,id等于link1的内容,二者需要空格分开。
print soup.select('p #link1') #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- elsie --></a>]
直接子标签查找
print soup.select("head > title") #[<title>the dormouse's story</title>]
(5).属性查找
查找时还可以加入属性元素,属性需要用中括号括起来,注意属性和标签属于同一节点,所以中间不能加空格,否则会无法匹配到。
print soup.select("head > title") #[<title>the dormouse's story</title>] print soup.select('a[href="http://example.com/elsie"]') #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- elsie --></a>]
属性仍然可以与上述查找方式组合,不在同一节点的空格隔开,同一节点的不加空格。
print soup.select('p a[href="http://example.com/elsie"]') #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- elsie --></a>]
from bs4 import beautifulsoup import requests f = requests.get(url,headers=headers) soup = beautifulsoup(f.text,'lxml') for k in soup.find_all('div',class_='pl2'): #找到div并且class为pl2的标签 b = k.find_all('a') #在每个对应div标签下找a标签,会发现,一个a里面有四组span n.append(b[0].get_text()) #取第一组的span中的字符串
示例如下:
# 使用 lxml 的 etree 库 from lxml import etree text = ''' <div> <ul> <li class="item-0"><a href="link1.html">first item</a></li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-inactive"><a href="link3.html">third item</a></li> <li class="item-1"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a> # 注意,此处缺少一个 </li> 闭合标签 </ul> </div> ''' #利用etree.html,将字符串解析为html文档 html = etree.html(text) # 按字符串序列化html文档 result = etree.tostring(html) print(result)
输出结果如下:
<html><body> <div> <ul> <li class="item-0"><a href="link1.html">first item</a></li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-inactive"><a href="link3.html">third item</a></li> <li class="item-1"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a></li> </ul> </div> </body></html>
可以看到。lxml会自动修改html代码。例子中不仅补全了li标签,还添加了body,html标签。
函数 | 描述 |
---|---|
json.dumps | 将python对象编码成json字符串 |
json.loads | 将已编码的json字符串解析为python对象 |
#!/usr/bin/python import json data = [ { 'name' : '张三', 'age' : 25}, { 'name' : '李四', 'age' : 26} ] jsonstr1 = json.dumps(data) #将python对象转为json字符串 jsonstr2 = json.dumps(data,sort_keys=true,indent=4,separators=(',',':')) #让json数据格式化输出,sort_keys:当key为文本,此值为true则按顺序打印,为false则随机打印 jsonstr3 = json.dumps(data, ensure_ascii=false) #将汉字不转换为unicode编码 print(jsonstr1) print('---------------分割线------------------') print(jsonstr2) print('---------------分割线------------------') print(jsonstr3)
输出结果:
[{"name": "\u5f20\u4e09", "age": 25}, {"name": "\u674e\u56db", "age": 26}] ---------------分割线------------------ [ { "age":25, "name":"\u5f20\u4e09" }, { "age":26, "name":"\u674e\u56db" } ] ---------------分割线------------------ [{"name": "张三", "age": 25}, {"name": "李四", "age": 26}]
#!/usr/bin/python import json data = [ { 'name' : '张三', 'age' : 25}, { 'name' : '李四', 'age' : 26} ] jsonstr = json.dumps(data) print(jsonstr) jsonobj = json.loads(jsonstr) print(jsonobj) # 获取集合第一个 for i in jsonobj: print(i['name'])
输出结果为:
[{"name": "\u5f20\u4e09", "age": 25}, {"name": "\u674e\u56db", "age": 26}] [{'name': '张三', 'age': 25}, {'name': '李四', 'age': 26}] 张三 李四`
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
python求numpy中array按列非零元素的平均值案例
网友评论