python3 爬虫6--requests的使用(1)
1用requests进行网页请求与urlopen差不多,这里省略不说
2抓取网页的学习
import requests
import re
headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'}
r=requests.get("https://www.zhuhu.com/explore",headers=headers)
#下面是正则表达式,我在之后学习,这里我看不懂。。
pattern = re.compile('explore-feed.*?question_link.*?>(.*?)</a>', re.S)
titles = re.findall(pattern, r.text)
print(titles)
上面是知乎发现网页抓取的例子,唯一需要注意的是get请求的时候需要添加Headers,不能直接get请求。
3抓取二进制数据
r=requests.get('http://github.com/favicon.ico')
print(r.text)
print(r.content)
抓取的目标是站点的图标
r.text返回的是字符串类型,返回的结果是文本文件
r.content如果返回的结果是图片、音频、视频文件,则获得字节流数据
with open('favicon.ico','wb') as f:
f.write(r.content)
这样就可以把图标文件存储到了favicon.ico文件中,同样的音频、视频文件也能这样获取。
4post请求
post请求可以像表单提交一样,将表单数据添加到链接中
data = {'name': 'germey', 'age': '22'}
r = requests.post("http://httpbin.org/post", data=data)
print(r.text)
5respones
我们可以通过get请求之后的respons获得更多的信息
例如
r=requests.get('http://www.jianshu.com')
print(r.status_code)
print(r.headers)
print(r.cookies)
print(r.url)
我们可以判断查询码判断请求是否成功
r = requests.get('http://www.jianshu.com')
exit() if not r.status_code == requests.codes.ok else print('Request Successfully')
print(r.history)
状态码存储在Spider-python文件夹中。
python3 爬虫6--requests的使用(1)的更多相关文章
- python3爬虫-使用requests爬取起点小说
import requests from lxml import etree from urllib import parse import os, time def get_page_html(ur ...
- python3爬虫-通过requests获取安居客房屋信息
import requests from fake_useragent import UserAgent from lxml import etree from http import cookiej ...
- python3爬虫之requests库基本使用
官方文档链接(中文) https://2.python-requests.org/zh_CN/latest/ requests 基于 urllib3 ,python编写. 安装 pip insta ...
- python3 爬虫相关-requests和BeautifulSoup
前言 时间的关系,这篇文章只记录了相关库的使用,没有进行深入分析,各位看官请见谅(还是因为懒.....) requests使用 发送无参数的get请求 r = requests.get('http:/ ...
- python3爬虫-通过requests爬取图虫网
import requests from fake_useragent import UserAgent from requests.exceptions import Timeout from ur ...
- python3爬虫-通过requests获取拉钩职位信息
import requests, json, time, tablib def send_ajax_request(data: dict): try: ajax_response = session. ...
- python3爬虫-通过requests爬取西刺代理
import requests from fake_useragent import UserAgent from lxml import etree from urllib.parse import ...
- python3 爬虫利用Requests 实现下载进度条
一.编写代码 from datetime import datetime,date,timedelta from contextlib import closing import urllib,url ...
- Python3爬虫使用requests爬取lol英雄皮肤
本人博客:https://xiaoxiablogs.top 此次爬取lol英雄皮肤一共有两个版本,分别是多线程版本和非多线程版本. 多线程版本 # !/usr/bin/env python # -*- ...
- python3 爬虫---爬取豆瓣电影TOP250
第一次爬取的网站就是豆瓣电影 Top 250,网址是:https://movie.douban.com/top250?start=0&filter= 分析网址'?'符号后的参数,第一个参数's ...
随机推荐
- IDEA 端口占用,启动失败,提示Web server failed to start. Port 8080 was already in use.
问题描述: 使用IDEA开发Spring Boot项目,今天启动提示端口占用,导致启动失败!(我昨天也是用的这个端口,可以正常启动) *************************** APPLI ...
- spring的事务是如何回滚的、事务传播?
实际上也是问的这个问题 spring的事务管理是如何实现的?总: spring的事务是由aop来实现的,首先要生成具体的代理对象,然后按照aop的整套流程来执行具体的操作逻辑,正常情况下要通过通知来 ...
- ASP.NET Core 6框架揭秘实例演示[09]:配置绑定
我们倾向于将IConfiguration对象转换成一个具体的对象,以面向对象的方式来使用配置,我们将这个转换过程称为配置绑定.除了将配置树叶子节点配置节的绑定为某种标量对象外,我们还可以直接将一个配置 ...
- Burp intruder暴力攻击web口令
实验目的 利用Burp intruder功能爆破出后台登陆密码admin. 实验原理 1)Burp Suite是Web应用程序测试的最佳工具之一,其多种功能可以帮我们执行各种任务.请求的拦截和修改,扫 ...
- [题解]Codeforces Round #519 - D. Mysterious Crime
[题目] D. Mysterious Crime [描述] 有m个n排列,求一共有多少个公共子段. 数据范围:1<=n<=100000,1<=m<=10 [思路] 对于第一个排 ...
- 简单excel饼状图怎么做,bi工具怎么做饼状图
饼状图是为了在一个整体体现个体所占的比例,比如一块蛋糕每人各分多大份.了解了饼状图的含义,就来学习饼状图怎么做吧. 首先,我们准备excel表格饼状图的初始数据 然后选择excel表格上方的插入,选择 ...
- 【基础概念】匹夫细说C#:不是“栈类型”的值类型,从生命周期聊存储位置
转载地址 https://www.cnblogs.com/murongxiaopifu/p/4419040.html 0x00 前言: 匹夫在日常和别人交流的时候,常常会发现一旦讨论涉及到" ...
- centos7 部署ansible
Ansible默认采用SSH的方式管理客户端,基于python开发,由paramiko和PyYAMl 两个关键模块构建 支持非root用户管理,支持sudo ansible作用:通过使用ansible ...
- WPF优秀组件推荐之FreeSpire
概述 Spire是一套可以轻松处理Word.Excel和PDF的商业组件,需要收费,但是他有一套对应的免费组件FreeSpire可以使用,免费组件在功能上有一些限制(比如:excel的sheet数量不 ...
- Cloud Computing Chapter3 (云计算第三章)
本篇文章是对课程大型软件系统设计与体系结构(双语)[又名:云计算]的课堂内容总结,适用于大连交通大学. Cloud Computing Chapter3 Understanding Cloud Com ...