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 ...
随机推荐
- rust连接oracle数据库遇到DPI-1047: Cannot locate a 64-bit Oracle Client library的解决方案
这两天要实现一个用rust连接远程的oracle数据库的需求,所以就需要用rust连接oracle. 在github上面找到一个库,地址:https://github.com/kubo/rust-or ...
- Objective-C 基础教程第三章,面向对象编程基础知
目录 Objective-C 基础教程第三章,面向对象编程基础知 0x00 前言 0x01 间接(indirection) 0x02 面向对象编程中使用间接 面向过程编程 面向对象编程 0x03 OC ...
- Understanding JSON Schema
json schema 在线校验器 译自:Understanding JSON Schema { "type": "object", "propert ...
- 企业为什么需要BI报表工具?
企业一旦成规模运作起来,数据量是数以万计的,无论数据还是报表都需要协同管理不断更新.面对各大OA\ERP\CRM系统的数据,传统用Excel处理数据效率是很低下的.要想快速出报表,快速取数,快速做业务 ...
- 【C# 程序集】把自己的程序集添加|卸载 到GAC 全局程序集缓存中
添加全局程序集缓存 Global Assembly Cache Tool (Gacutil.exe) 操作环境windows10+vs2019 cmd.exe位置在 C:\Windows\System ...
- Specified cast is not valid(C#) 引发的思考(装箱,拆箱本质)
没有很华丽的语言,直接拿代码说事情把. 这段代码,会报错吗? 结论:当然不会 这段代码会报错了.原因是为啥? 这里面的水比较深.也要提醒各位写代码的适合要引起注意.异常:System.Invalid ...
- c++ cgi执行shell命令l发送qq邮件mailx
#include <cstdlib> //随机数 #include <iostream> #include <cstdio> //popen函数调用的需要 #inc ...
- WPS:添加公式后,行间距变宽的解决方法
找到公式所属段落的样式,右键修改样式 左下角'格式'中选择'段落' 段落间距设置为0,不要勾选与文档网格对齐
- 关于LR的笔记
- LeetCode-039-组合总和
组合总和 题目描述:给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数 ...