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. Grafana 系列文章(四):Grafana Explore

    ️URL: https://grafana.com/docs/grafana/latest/explore/ Description: Explore Grafana 的仪表盘 UI 是关于构建可视化 ...

  2. 在线程里使用线程外的变量为什么一定要是final类型

    public class CyclicBarrierDemo { public static void main(String[] args) { /* * 七龙珠 * */ CyclicBarrie ...

  3. 用XAMPP搭建本地:Web服务器,访问服务器,下载服务器。

    用XAMPP搭建本地:Web服务器,访问服务器,下载服务器. 首先需要下载XAMPP,链接为:XAMPP下载地址,XAMPP中文网. 下载完成后进行安装,直接一键点到底. 一.如何确定我们安装完成了? ...

  4. QtQuick使用MediaPlayer抓取摄像头影响报错Error: "Your GStreamer installation is missing a plug-in."

    环境:ubuntu18.04 Qt5.9.5 描述:项目需要使用qtquick作为显示界面用于播放从网络摄像头抓取的影像,海康网络摄像头,摄像头源协议使用的是rtsp,影像数据格式为x-h264,但在 ...

  5. Windows性能监控工具Perfmon的使用、性能指标分析

    Fighting_001 关注  0.1 2018.08.25 22:18* 字数 1488 阅读 7604评论 0喜欢 4 目录结构 一.Perfmon简介.性能监控指标.性能对象指标 1.常用的性 ...

  6. 走进Linux

    走进Linux Linux诞生 ​ BSD,Unix,Minix都是操作系统. ​ 1991年,林纳斯托瓦兹在上大学时,对操作系统很好奇.但是由于 386 BSD 还没有出来.可是他不喜欢他的 386 ...

  7. 基于APIView写接口

    一.视图层代码 """ 基于APIView实现接口的编写 用的是同一个模型表 路由也没变 这次做了解耦合 写了序列化类 与视图类分开了 """ ...

  8. 明解STM32—GPIO理论基础知识篇之基本结构

    ​ 一.前言 万物皆有源头,大家学习单片机的源头操作就是通过GPIO口点灯,GPIO作为STM32最基础的外设,也是大家最先接触的外设.当然,看似基础的GPIO,不仅仅是简单的设置好IO口,让灯亮起就 ...

  9. LoginServlet类

    import cn.itcast.dao.UserDao; import cn.itcast.domain.User; import javax.servlet.ServletException; i ...

  10. C++练习6 不同参数的传递方式

    当函数的形参是变量时,函数内的操作是只对形参的操作,并不会对实参造成影响 当函数的形参是引用时,在函数内对形参操作的同时也会对实参造成影响 1 #include <iostream> 2 ...