1、爬取页面,打印页面信息

 1 import requests
2
3 # get请求
4 response_get=requests.get("https://www.baidu.com") # 生成一个response对象
5 response_get.encoding=response_get.apparent_encoding # 设置编码格式
6
7 # post请求
8 response_post = requests.post("http://httpbin.org/post")
9 response_post.encoding=response_post.apparent_encoding
10
11 print("抓取百度网页html内容如下(get请求):")
12 print(response_get.text)
13 print("抓取百度网页html内容如下(post请求):")
14 print(response_post.text)

2、关于反爬机制页面的处理

 1 # 关于绕过反爬机制
2 response_get=requests.get("http://www.zhihu.com") # 生成一个response对象
3 response_get.encoding=response_get.apparent_encoding # 设置编码格式
4 print("不设置头信息,状态码:",str(response_get.status_code))
5 print("抓取网页html内容如下(get请求):")
6 print(response_get.text)
7
8 # 设置User-Agent,添加头部信息,伪装浏览器
9 headers={
10 "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36"
11 }
12 response_get=requests.get("https://www.zhihu.com",headers=headers)
13 response_get.encoding=response_get.apparent_encoding
14 print("设置头信息,状态码:",str(response_get.status_code))
15 print("抓取网页html内容如下(get请求):")
16 print(response_get.text)

3、爬取信息并保存到本地方法

 1 import requests
2
3 # get请求
4 response_get = requests.get("http://www.baidu.com") # 生成一个response对象
5 response_get.encoding = response_get.apparent_encoding # 设置编码格式
6 print("抓取网页html内容如下(get请求):")
7 print(response_get.text)
8 # 爬取信息并保存到本地方法1:
9 with open("./file/zhongyan.html", "w", encoding="utf-8") as f:
10 f.write(response_get.text)
11 f.close()
12 # 爬取信息并保存到本地方法2:
13 file = open("./file/zhongyan1.html", "w", encoding="utf-8")
14 file.write(response_get.text)
15 file.close()

4、美化爬出html信息

1 import requests
2 from bs4 import BeautifulSoup
3
4 # get请求
5 response_get = requests.get("http://www.baidu.com") # 生成一个response对象
6 response_get.encoding = response_get.apparent_encoding # 设置编码格式
7 print("抓取网页html内容如下(get请求):")
8 soup=BeautifulSoup(response_get.text,"html.parser")
9 print(soup.prettify())

5、整体代码如下:

 1 import requests
2 from bs4 import BeautifulSoup
3
4 # get请求
5 headers = {
6 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36"
7 }
8 response_get = requests.get("http://www.baidu.com", headers=headers) # 生成一个response对象
9 response_get.encoding = response_get.apparent_encoding # 设置编码格式
10 print("抓取网页html内容如下(get请求):")
11 # 美化爬出数据展示
12 soup = BeautifulSoup(response_get.text, "html.parser")
13 # prettify()每逢标签,自动换行
14 print(soup.prettify())
15 # 爬取信息并保存到本地方法1:
16 with open("./file/baidu.html", "w", encoding="utf-8") as f:
17 f.write(soup.prettify())
18 f.close()
19 # 爬取信息并保存到本地方法2:
20 file = open("./file/baidu1.html", "w", encoding="utf-8")
21 file.write(soup.prettify())
22 file.close()

网络爬虫Python(一)的更多相关文章

  1. python网络爬虫-python基础(三)

    python安装 Anaconda的python科学计算环境,只需要想普通软件一样安装就可以把python的环境变量.解释器.开发环境都安装到计算机中 除此之外anaconda还提供众多的科学计算的包 ...

  2. Python网络爬虫与如何爬取段子的项目实例

    一.网络爬虫 Python爬虫开发工程师,从网站某一个页面(通常是首页)开始,读取网页的内容,找到在网页中的其它链接地址,然后通过这些链接地址寻找下一个网页,这样一直循环下去,直到把这个网站所有的网页 ...

  3. Python网络爬虫与信息提取笔记

    直接复制粘贴笔记发现有问题 文档下载地址//download.csdn.net/download/hide_on_rush/12266493 掌握定向网络数据爬取和网页解析的基本能力常用的 Pytho ...

  4. Python初学者之网络爬虫(二)

    声明:本文内容和涉及到的代码仅限于个人学习,任何人不得作为商业用途.转载请附上此文章地址 本篇文章Python初学者之网络爬虫的继续,最新代码已提交到https://github.com/octans ...

  5. [Python] 网络爬虫和正则表达式学习总结

    以前在学校做科研都是直接利用网上共享的一些数据,就像我们经常说的dataset.beachmark等等.但是,对于实际的工业需求来说,爬取网络的数据是必须的并且是首要的.最近在国内一家互联网公司实习, ...

  6. 智普教育Python培训之Python开发视频教程网络爬虫实战项目

    网络爬虫项目实训:看我如何下载韩寒博客文章Python视频 01.mp4 网络爬虫项目实训:看我如何下载韩寒博客文章Python视频 02.mp4 网络爬虫项目实训:看我如何下载韩寒博客文章Pytho ...

  7. python之网络爬虫

    一.演绎自已的北爱 踏上北漂的航班,开始演奏了我自已的北京爱情故事 二.爬虫1 1.网络爬虫的思路 首先:指定一个url,然后打开这个url地址,读其中的内容. 其次:从读取的内容中过滤关键字:这一步 ...

  8. 读书笔记汇总 --- 用Python写网络爬虫

    本系列记录并分享:学习利用Python写网络爬虫的过程. 书目信息 Link 书名: 用Python写网络爬虫 作者: [澳]理查德 劳森(Richard Lawson) 原版名称: web scra ...

  9. Python即时网络爬虫项目启动说明

    作为酷爱编程的老程序员,实在按耐不下这个冲动,Python真的是太火了,不断撩拨我的心. 我是对Python存有戒备之心的,想当年我基于Drupal做的系统,使用php语言,当语言升级了,推翻了老版本 ...

  10. python Cmd实例之网络爬虫应用

    python Cmd实例之网络爬虫应用 标签(空格分隔): python Cmd 爬虫 废话少说,直接上代码 # encoding=utf-8 import os import multiproces ...

随机推荐

  1. vue3和百度地图关键字检索 定位 点击定位

    效果图 在index.html中引入 百度地图开放平台 去申请你的ak 非常的简单可以自己百度 一下 <!-- 这个用官网给的有好多警告 更具百度的把 https://api.map.baidu ...

  2. MySQL数据库技术实战

    MySQL数据库技术实战   一,安装mysql 很早之前就知道mysql提供了一套数据库样本(github地址),用于测试你的应用程序和数据库服务器. 今天分享下使用过程并将他发布到了码云,以便于同 ...

  3. DevGridView表格导出自定义页脚

    在CustomSummaryCalculate 事件里 //强制初始化,如果列是动态通过数据源加载的,则绑定数据源后gridview想获取列的话,需要调用强制初始化的方法 view.GridContr ...

  4. excel空格处理

    private String StringTrim(String str){ return str.replaceAll("[\\s\\u00A0]+","") ...

  5. Rust 的PIN的作用图

  6. Windows MongoDB的安装及配置图文说明(非常详细)

    1.下载 MongoDB 预编译二进制包下载地址:https://www.mongodb.com/try/download/community 2.安装 1)选择接受许可协议,点击Next下一步: 2 ...

  7. 视觉十四讲:第六讲_ceres非线性优化

    使用Ceres求解非线性优化问题,一共分为三个部分: 1. 第一部分:构建cost fuction,即代价函数,也就是寻优的目标式.这个部分需要使用仿函数(functor)这一技巧来实现,做法是定义一 ...

  8. 一个比 Redis 性能更强的数据库

    给大家推荐一个比Redis性能更强的数据:KeyDB KeyDB是Redis的高性能分支,侧重于多线程.内存效率和高吞吐量.除了性能改进外,KeyDB还提供主动复制.闪存和子密钥过期等功能.KeyDB ...

  9. PHY状态机分析

    PHY的12种状态 enum phy_state { PHY_DOWN = 0, //关闭网卡 PHY_STARTING, //PHY设备准备好了,PHY driver尚为准备好 PHY_READY, ...

  10. Luogu P1505.[国家集训队]旅游

    题解 真真正正是个码农题,不过很套路,熟练就打得很快,不过要用点维护边的信息在 \(\text{LCA}\) 出要注意,不能处理此点的信息 \(Code\) #include<cstdio> ...