网页解析库--lxml

from bs4 import BeautifulSoup
from lxml import etree

doc='''<html>
	<body>
		<title>
			A story
		</title>
		<p class="title">
			<b>
				Story begin!
			</b>
		</p>
		<p class="story">
			Once upon a time there were three little sister.
			<a href="http://example.com/elsie">
				class="sister" id="link1" &gt;Elsie
			</a>
			,
			<a href="http://example.com/lacie">
				class="sister" id="link2" &gt;Lacie
			</a>
			;
			and they lived in a castle
		</p>
	</body>
</html>'''
soup=BeautifulSoup(doc,"lxml")
s=soup.prettify()
print(s)

查找title元素

soup=BeautifulSoup(doc,"lxml")
tag=soup.find("title")
print(type(tag),tag)

查找所有a元素

soup=BeautifulSoup(doc,"lxml")
tags=soup.findAll("a")
for tag in tags:
    print(tag)

查找含titie的所有p元素

soup=BeautifulSoup(doc,"lxml")
tags=soup.findAll("p",attrs={"class":"title"})
print(tags)

查找class为sister的元素

soup=BeautifulSoup(doc,"lxml")
tags=soup.findAll(name=None,attrs={"class":"sister"})
print(tags)

去掉tag元素的信息

soup=BeautifulSoup(doc,"lxml")
tags=soup.find_all("a")
for tag in tags:
    print(tag.text)

如果tag还含有子节点

soup=BeautifulSoup(doc,"lxml")
tags=soup.find_all("p")
for tag in tags:
    print(tag.text)

利用select方法

 CSS 时,标签名不加任何修饰

 名(class="className"引号内即为类名)前加点

  id名(id="idName"引号前即为id名)前加 #

 于是可以利用类似的方法来筛选元素,比如 soup.select(),返回类型是 list

 print soup.select('title')

 [<title>The Dormouse's story</title>]

爬取图片

from bs4 import BeautifulSoup
import requests
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"}
user_path="https://www.pexels.com/search/"
word=input("请输入你要下载的图片:")
url=user_path+word+'/'
wb_data=requests.get(url,headers=headers)
soup=BeautifulSoup(wb_data.text,'lxml')
imgs=soup.select('article>a>img')
list=[]
for img in imgs:
    photo=img.get('src')
    list.append(photo)

 保存图片

from bs4 import BeautifulSoup
from lxml import etree
import requests
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"}
user_path="https://www.pexels.com/search/"
word=input("请输入你要下载的图片:")
url=user_path+word+'/'
wb_data=requests.get(url,headers=headers)
soup=BeautifulSoup(wb_data.text,'lxml')
imgs=soup.select('article>a>img')
list=[]
for img in imgs:
    photo=img.get('src')
    list.append(photo)
    print(photo)
path="C://Users/14760/Desktop/photo/"
for item in list:
    data=requests.get(item)
    fp=open(path+item.split("?")[0][-10],'wb')
    fp.write(data.content)
    fp.close()
    

w      以写的方式打开(会覆盖原有的文件)

r      以只读的方式打开

a      以追加的模式打开(在原文件的末尾追加要写入的数据,不覆盖原文件)

b      以二进制文件的方式打开

r+   w+  a+  都是以读写的方式打开

rb  以二进制读的方式打开

wb  以二进制写的方式打开

ab  以二进制追加的模式打开

rb+  wb+  ab+  以二进制读写的方式打开·

 

保存图片

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值