前言:

  • 环境配置:windows64、python3.4
  • requests库基本操作:

1、安装:pip install requests

2、功能:使用 requests 发送网络请求,可以实现跟浏览器一样发送各种HTTP请求来获取网站的数据。

3、命令集操作:

import requests  # 导入requests模块

r = requests.get("https://api.github.com/events")  # 获取某个网页

# 设置超时,在timeout设定的秒数时间后停止等待响应
r2 = requests.get("https://api.github.com/events", timeout=0.001) payload = {'key1': 'value1', 'key2': 'value2'}
r1 = requests.get("http://httpbin.org/get", params=payload) print(r.url) # 打印输出url print(r.text) # 读取服务器响应的内容 print(r.encoding) # 获取当前编码 print(r.content) # 以字节的方式请求响应体 print(r.status_code) # 获取响应状态码
print(r.status_code == requests.codes.ok) # 使用内置的状态码查询对象 print(r.headers) # 以一个python字典形式展示的服务器响应头
print(r.headers['content-type']) # 大小写不敏感,使用任意形式访问这些响应头字段 print(r.history) # 是一个response对象的列表 print(type(r)) # 返回请求类型
  • BeautifulSoup4库基本操作:

1、安装:pip install BeautifulSoup4

2、功能:Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库。

3、命令集操作:

 import requests
from bs4 import BeautifulSoup
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><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" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p> <p class="story">...</p>
""" ss = BeautifulSoup(html_doc,"html.parser")
print (ss.prettify()) #按照标准的缩进格式的结构输出
print(ss.title) # <title>The Dormouse's story</title>
print(ss.title.name) #title
print(ss.title.string) #The Dormouse's story
print(ss.title.parent.name) #head
print(ss.p) #<p class="title"><b>The Dormouse's story</b></p>
print(ss.p['class']) #['title']
print(ss.a) #<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
print(ss.find_all("a")) #[。。。]
print(ss.find(id = "link3")) #<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a> for link in ss.find_all("a"):
print(link.get("link")) #获取文档中所有<a>标签的链接 print(ss.get_text()) #从文档中获取所有文字内容
 import requests
from bs4 import BeautifulSoup html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><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" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p> <p class="story">...</p>
"""
soup = BeautifulSoup(html_doc, 'html.parser') # 声明BeautifulSoup对象
find = soup.find('p') # 使用find方法查到第一个p标签
print("find's return type is ", type(find)) # 输出返回值类型
print("find's content is", find) # 输出find获取的值
print("find's Tag Name is ", find.name) # 输出标签的名字
print("find's Attribute(class) is ", find['class']) # 输出标签的class属性值 print(find.string) # 获取标签中的文本内容 markup = "<b><!--Hey, buddy. Want to buy a used parser?--></b>"
soup1 = BeautifulSoup(markup, "html.parser")
comment = soup1.b.string
print(type(comment)) # 获取注释中内容
  • 小试牛刀:
 import requests
import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030') #改变标准输出的默认编码 r = requests.get('https://unsplash.com') #像目标url地址发送get请求,返回一个response对象 print(r.text) #r.text是http response的网页HTML

参考链接:

https://blog.csdn.net/u012662731/article/details/78537432

http://www.cnblogs.com/Albert-Lee/p/6276847.html

https://blog.csdn.net/enohtzvqijxo00atz3y8/article/details/78748531

python爬虫之requests+selenium+BeautifulSoup的更多相关文章

  1. 孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块

    孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块 (完整学习过程屏幕记录视频地址在文末) 从今天起开始正式学习Python的爬虫. 今天已经初步了解了两个主要的模块: ...

  2. Python爬虫练习(requests模块)

    Python爬虫练习(requests模块) 关注公众号"轻松学编程"了解更多. 一.使用正则表达式解析页面和提取数据 1.爬取动态数据(js格式) 爬取http://fund.e ...

  3. python爬虫动态html selenium.webdriver

    python爬虫:利用selenium.webdriver获取渲染之后的页面代码! 1 首先要下载浏览器驱动: 常用的是chromedriver 和phantomjs chromedirver下载地址 ...

  4. Python爬虫之设置selenium webdriver等待

    Python爬虫之设置selenium webdriver等待 ajax技术出现使异步加载方式呈现数据的网站越来越多,当浏览器在加载页面时,页面上的元素可能并不是同时被加载完成,这给定位元素的定位增加 ...

  5. python爬虫数据解析之BeautifulSoup

    BeautifulSoup是一个可以从HTML或者XML文件中提取数据的python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式. BeautfulSoup是python爬虫三 ...

  6. python爬虫之requests库

    在python爬虫中,要想获取url的原网页,就要用到众所周知的强大好用的requests库,在2018年python文档年度总结中,requests库使用率排行第一,接下来就开始简单的使用reque ...

  7. python爬虫(7)——BeautifulSoup

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

  8. python爬虫之初始Selenium

    1.初始 Selenium[1]  是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, 8, 9, 10, 11),Moz ...

  9. PYTHON 爬虫笔记七:Selenium库基础用法

    知识点一:Selenium库详解及其基本使用 什么是Selenium selenium 是一套完整的web应用程序测试系统,包含了测试的录制(selenium IDE),编写及运行(Selenium ...

随机推荐

  1. CEF3研究(四)之javascript集成

    一.介绍 谷歌浏览器和CEF使用V8JavaScript Engine作为内容的JavaScript实现.在浏览器中的每个窗口都有它自己在的JS上下文提供作用域和在窗口中安全的执行JS代码.CEF暴露 ...

  2. 【APUE】进程间通信之信号量

    信号量是一个计数器,用于多进程对共享数据对象的访问 为了获得共享资源,进程需要执行下列操作: 1)测试控制该资源的信号量 2)若此信号量为正,则进程可以使用该资源,进程将信号量减1,表示它使用了一个资 ...

  3. ext.net 2.5 导出excel的使用方法

    前台页面的导入,导出 <ext:FileUploadField ID="FileUploadField_Import" runat="server" Bu ...

  4. :>/dev/null 2>&1 的作用

    shell中可能经常能看到:>/dev/null 2>&1 命令的结果可以通过%>的形式来定义输出 /dev/null 代表空设备文件 > 代表重定向到哪里,例如:ec ...

  5. APUE 线程 - 程序清单

    APUE 线程 - 程序清单 程序清单11-1 打印线程ID #include "util.h" #include<pthread.h> pthread_t ntid; ...

  6. java-组合优于继承

    组合和继承.都能实现对类的扩展. 差别例如以下表所看到的 组合 继承 has-a关系 is-a关系 执行期决定 编译期决定 不破坏封装,总体和局部松耦合 破坏封装,子类依赖父类 支持扩展,任意添加组合 ...

  7. 【iOS系列】-UITableView的使用

    UITableView的使用: 第一:数据展示条件 1,UITableView的所有数据都是由数据源(dataSource)提供,所以想在UITableView展示数据,必须设置UITableview ...

  8. 树莓派的PWM脉宽调制功能介绍

    近期想用树莓派控制航模的电调,于是研究了下PWM.貌似控制电调比較麻烦,由于电调须要发送几个特定的信号启动,然后才干进入控制模式.今天先弄明确PWM,慢慢折腾.以下的程序亲測可用,我用的树莓派mode ...

  9. HDFS集中式缓存管理(Centralized Cache Management)

    Hadoop从2.3.0版本号開始支持HDFS缓存机制,HDFS同意用户将一部分文件夹或文件缓存在HDFS其中.NameNode会通知拥有相应块的DataNodes将其缓存在DataNode的内存其中 ...

  10. Codeforces Round #419 (Div. 1) C. Karen and Supermarket 树形DP

    C. Karen and Supermarket     On the way home, Karen decided to stop by the supermarket to buy some g ...