前言:

  • 环境配置: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. 表皮囊肿?wtf

    https://baike.baidu.com/item/%E8%A1%A8%E7%9A%AE%E5%9B%8A%E8%82%BF/7852024?fr=aladdin

  2. 解决安装oracle11g r2时提示pdksh conflicts with ksh-20100621-2.el6.i686问题

    http://blog.csdn.net/linghao00/article/details/7943740 http://www.2cto.com/os/201306/218566.html 在Ce ...

  3. 【algorithm】尾递归

    尾递归和一般的递归不同在对内存的占用,普通递归创建stack累积而后计算收缩,尾递归只会占用恒量的内存(和迭代一样).SICP中描述了一个内存占用曲线,用以上答案中的Python代码为例(普通递归): ...

  4. Centos5.11 //IP/phpmyadmin 远程无法登入

    异地登入phpmyadmin时,会出现"You don't have permission to access /phpmyadmin/ on this server."这是因为配 ...

  5. [React] Use the Fragment Short Syntax in Create React App 2.0

    create-react-app version 2.0 added a lot of new features. One of the new features is upgrading to Ba ...

  6. 一天教你入门struts2

    写在前面 自己也是一个java和java web的菜鸟.之前没有接触过java web方面的开发 想通过一个小项目,来熟悉struts2的开发流程 一个有趣的想法源于教研室项目上的一个功能实现–自己主 ...

  7. DataGridView依据下拉列表显示数据

    我们都知道,DataGridView能够直接绑定数据源.显示数据库中的数据.可是我想做的是能够对他进行条件查询,依据用户级别选择不同级别的记录. 以上这个控件就是DataGridView控件,能够用它 ...

  8. Office WORD如何为每一页设置不同的页眉页脚

    如下图所示,我想要为封面和目录,摘要等等设置不同的页眉页脚(一般封面和目录不需要页脚)   而从正文开始,套用相同的页眉和以页数作为页脚(注意"第一章 绪论"不是这个文档的第一页) ...

  9. smartfoxserver扩展里面过滤聊天的不合法字符

    http://blog.csdn.net/yc7369/article/details/35567105 近期做手游客户要求加上一个聊天功能.事实上嘛,个人认为这个聊天功能比較鸡肋,这部分差点儿已经有 ...

  10. 【CODEFORCES】 C. Dreamoon and Strings

    C. Dreamoon and Strings time limit per test 1 second memory limit per test 256 megabytes input stand ...