Beautiful Soup 4.4.0 中文文档:http://beautifulsoup.readthedocs.io/zh_CN/latest/

#coding:utf-8
from bs4 import BeautifulSoup
import requests, os
import urllib3
#禁用安全请求警告
urllib3.disable_warnings()

#===============以下是爬取博客园首页摘要====================
r=requests.get("https://www.cnblogs.com/xiaohuhu/")
#请求首页后获取整个html界面
blog=r.content
#print(blog)
#用html.parser解析html
soup=BeautifulSoup(blog,"html.parser")

#获取所有标题日期,class属性为dayTitle,返回列表
times=soup.find_all(class_='dayTitle')

#获取所有摘要标题,class属性为postTitle
titles=soup.find_all(class_="postTitle")

#获取所有摘要内容,class属性为postCon
descs = soup.find_all(class_="postCon")
 
for i,j,k in zip(times,titles,descs):
    print(i.a.string)  #打印a标签的文本信息
    print(j.a.string)  
    print(k.div.contents[0])  #标签div的contents属性可以将标签的子节点以列表的方式输出,这里选取第一个

#===============以下是爬取糗事百科段子====================

r = requests.get("https://www.qiushibaike.com/", verify=False)
qiubai = r.content
soup = BeautifulSoup(qiubai, "html.parser")
duanzi = soup.find_all(class_="content")
 
for i in duanzi:
    # 标签span的 contents 属性可以将标签的子节点以列表的方式输出
    duan = i.span.contents[0]  # 取第一个
    print(duan)

#==============以下是爬取网页图片,并保存到本地==============

r = requests.get("http://699pic.com/sousuo-218808-13-1.html")
fengjing = r.content
soup = BeautifulSoup(fengjing, "html.parser")
# 找出所有的标签
images = soup.find_all(class_="lazy")
# print images  # 返回list对象
 
for i in images:
    try:
        jpg_rl = i["data-original"]   # 获取url 地址
        title = i["title"]           # 返回title名称

  #保存图片,os.getcwd()这个方法可以获取当前脚本的路径,requests.get打开图片的 url地址,content 方法返回的是二进制流文件,可以直接写到本地
        with open(os.getcwd()+"\\jpg\\"+title+'.jpg',"wb") as f:
            f.write(requests.get(jpg_rl).content)
    except:
        pass

BeautifulSoup实例的更多相关文章

  1. python BeautifulSoup实例测验

  2. python爬虫(7)——BeautifulSoup

    今天介绍一个非常好用的python爬虫库--beautifulsoup4.beautifulsoup4的中文文档参考网址是:http://beautifulsoup.readthedocs.io/zh ...

  3. 利用requests, beautifulsoup包爬取股票信息网站

    这是第一次用requests, beautifulsoup实现爬虫,此次爬取的是一个股票信息网站:http://www.gupiaozhishi.net.cn. 实现非常简单,只是为了demo使用的数 ...

  4. 第14.8节 Python中使用BeautifulSoup加载HTML报文

    一. 引言 BeautifulSoup是一个三方模块bs4中提供的进行HTML解析的类,可以认为是一个HTML解析工具箱,对HTML报文中的标签具有比较好的容错识别功能.阅读本节需要了解html相关的 ...

  5. Python开发简单爬虫 - 慕课网

    课程链接:Python开发简单爬虫 环境搭建: Eclipse+PyDev配置搭建Python开发环境 Python入门基础教程 用Eclipse编写Python程序   课程目录 第1章 课程介绍 ...

  6. Python开发简单爬虫(一)

    一 .简单爬虫架构: 爬虫调度端:启动爬虫,停止爬虫,监视爬虫运行情况 URL管理器:对将要爬取的和已经爬取过的URL进行管理:可取出带爬取的URL,将其传送给“网页下载器” 网页下载器:将URL指定 ...

  7. Forward团队-爬虫豆瓣top250项目-项目总结

    托管平台地址:https://github.com/xyhcq/top250 小组名称:Forward团队 组长:马壮 成员:李志宇.刘子轩.年光宇.邢云淇.张良 我们这次团队项目内容是爬取豆瓣电影T ...

  8. net spider(python 网络爬虫)

    # -*- coding: utf-8 -*- import urllib2,cookielib from bs4 import BeautifulSoup url="http://www. ...

  9. bs4爬虫入门

    # -*- coding: utf-8 -*- """ Created on Fri Nov 16 13:35:33 2018 @author: zhen "& ...

随机推荐

  1. 洛谷 P2398 GCD SUM || uva11417,uva11426,uva11424,洛谷P1390,洛谷P2257,洛谷P2568

    https://www.luogu.org/problemnew/show/P2398 $原式=\sum_{k=1}^n(k\sum_{i=1}^n\sum_{j=1}^n[(i,j)=k])$ 方法 ...

  2. Android逆向分析工具表

    逆向分析工具表 工具 描述 网址 androidterm Android Terminal Emulator http://code.google.com/p/androidterm/ droidbo ...

  3. 18.3.2从Class上获取信息(注解)

    package d18_3_1; /** * Class类上所包含的注解 * * getAnnotation(Class annotationClass) 获取该元素上指定的类型的注解 * getAn ...

  4. Could not open logfile" occurred when run "datapatch -verbose"

    CAUSE Due to Bug 25459405 - DATAPATCH FAILS WITH SP2-0768 IF NLS_LANGUAGE IS NOT SET TO AMERICANwhic ...

  5. c8051单片机注意事项:

    一定要注意交叉开关问题:外设要想正确分配到指定引脚,一定要用配置工具确定分配到指定引脚:如果手动分配一定要仔细验证.这方面有个深刻的教训. 有个项目用c8051f020,用到2个串口,硬件已经确定好了 ...

  6. CentOS 安装图形化界面方法

    登录系统,使用yum 安装 #yum groupinstall 'X Window System'  -y 安装GNOME桌面环境 #yum groupinstall  'GNOME Desktop ...

  7. AJPFX关于java的依赖 关联 聚合的关系解释

    依赖:  两个相对独立的系统,当一个系统要构筑另一个系统的实例,或者依赖另一的服务时,这两个就是依赖关系.比如自行车和打气筒之间就是依赖关系.代码表现形式如下:    public class A{  ...

  8. gp服务输出的结果文件输出到绝对路径

    gp服务跟本地用arcmap执行gp有个不同,就是输出的文件一般只能输出到arcgis server默认的output目录里面(arcgis server有此限制,无论怎么配还是写到output目录里 ...

  9. java8的lambda表达式,将List<DTO> 转为 List<DO>

    将List<PhoneDTO>转为List<PhoneDO>,通过java8的lambda表达式来操作,比传统的for循环精简很多: /** * List<PhoneDT ...

  10. 自欺欺人的使用 NSTimer 销毁

    自欺欺人的使用 NSTimer 销毁 Demo地址 1.NSTimer是要加到runloop中才会起作用. 常见的创建timer方式 // 第一种方式 @property (nonatomic , s ...