python之Beautiflusoup操作
2024-08-31 20:22:43
from bs4 import BeautifulSoup
import requests
import os ######对风景进行爬出操作
r = requests.get("http://699pic.com/sousuo-218808-13-1-0-0-0.html")
fengjing = r.content
soup = BeautifulSoup(fengjing,"html.parser")
#print(soup.prettify())
#找出所有class标签
images = soup.find_all(class_="lazy")
print(images)
for item in images:
try:
jpg_url = item["data-original"]
title = item["title"]
print(title)
print(jpg_url)
print("")
#保存图片
with open(os.getcwd()+"\\jpg"+title+".jpg","wb") as f:
f.write(requests.get(jpg_url).content)
except Exception as e:
pass
另外爬虫Blog如下程序:
from bs4 import BeautifulSoup
import requests #r = requests.get("https://www.cnblogs.com/Teachertao/")
# 请求首页后获取整个 html 界面
blog = r.content
#print(blog) #用html.parser解析出html
soup = BeautifulSoup(blog,"html.parser")
# prettify()可以自动解析为html格式
print(soup.prettify())
#获取所有的class属性为"block_title",返回Tag类
time = soup.find_all(class_="block_title")
#print(time) db = [item for item in time]
print(db)
# 获取title
title = soup.find_all(class_="posttitle")
print(title)
#获取摘要
desc = soup.find_all(class_="c_b_p_desc")
print(desc)
for item in desc:
# tag 的 .contents 属性可以将 tag 的子节点以列表的方式输出
print(item)
########标签如下截图:
########运行结果展示
最新文章
- C#位运算讲解与示例
- Empire C:Basic 4
- Java编程思想学习(九) 异常处理
- C++ 构造函数放置默认转换explicit关键字(2)
- Windows蓝屏后产生的.dmp分析原因
- [INS-41112] Specified network interface doesnt maintain connectivi
- 用C#对ADO.NET数据库完成简单操作
- Dedecms当前位置{dede:field name='position'/}修改
- Swift 字符串连接
- poj 1149
- VC2008下使用OpenSSL 1.0.0g(免编译)
- 将缓冲区的数字字符串转化成BCD码数据_INT PubNumericToBCDStr(_UCHR *pcNStr, _INT iNLen, _UCHR *pcBCDStr)
- C语言第一次博客作业 陈张鑫
- 【Unity Shaders】Vertex Magic —— 访问顶点颜色
- SQL删除指定条件的重复数据,只保留一条
- 【Unity】2.9 光源(Lights)
- vc6.0 PK vs2010
- sql server 2012 数据库日志文件过大,怎么缩小?
- go 运行项目
- 三星c7换屏幕教程
热门文章
- IntelliJ IDEA配置本地Tomcat方法---亲测有效
- spark streaming 踩过的那些坑
- fd最大值和限制
- [NOIP2005] 提高组 洛谷P1054 等价表达式
- Uva -1515 Pool construction(最小割)
- [bzoj1485][HNOI2009]有趣的数列_卡特兰数_组合数
- 何为幻读?MySQL又是如何解决幻读的?
- Office 针式打印机如何调节边距
- msyql索引详解
- 图像处理之基础---图像缩放中的双线性插值c实现