admin管理员组

文章数量:1530363

安装好pycharm,开始学习。

目录

  • 一、学习requests模块
    • 1.安装requests
    • 2.requests模块的简单使用
    • 3.requests发送请求的例子
  • 二、学习response响应对象
    • 1.text和content的区别
    • 2.其他属性或方法
  • 三、发送带header的请求头
    • dubug :User-Agent
  • 四、发送带参数的请求
    • 方法一:url中直接带参数
    • 方法二:使用params参数
  • 五、在headers中设置cookies参数
  • 六、使用Cookies保持会话
  • 七、CookieJar的对象与Cookie字典的互换
  • 八、超时参数timeout的使用
  • 九、代理proxies使用
  • 十、遇到的小问题
    • 1.如何调整pycharm背景
    • 2. pycharm自动换行
    • 3.pycharm显示行数
    • 4.github上不去

一、学习requests模块

1.安装requests

方法一:在pycharm创建一个新的项目之后,点入Terminal,使用pip install requests命令安装。

方法二:在cmd命令窗口中安装。

2.requests模块的简单使用

requests 提供了几种HTTP请求方式:GET、POST、HEAD、OPTIONS、PUT、PATCH、DELETE。

3.requests发送请求的例子

在mian.py文件中输入:

#导入
import requests

url = 'http//www.baidu'

response = requests.get(url)

# 打印源码的str类型数据
print(response.text)

二、学习response响应对象

1.text和content的区别

response.text=response.content.decode(‘推测出的编码字符集’)
response.text 的类型是str,是编码字符集解码后的结果。
response.content 的类型是byte。可以解决中文乱码问题。默认使用uft-8,如下。
或者使用 response.content(‘GBK’)

方法1:

import requests
url = 'http://www.baidu'
response = requests.get(url)

#方法一:(使用utf8可以显示title的中文)
response.encoding = 'utf8'
print(response.text)

方法2:

import requests
url = 'http://www.baidu'
response = requests.get(url)

#方法二:(是刚才方法一的合并,一句话更加简洁)
response.encoding = 'utf8'
print(response.content.decode())

输出结果如下:

2.其他属性或方法

代码示例如下:

import requests
url = 'http://www.baidu'
response = requests.get(url)

#response属性:
# 1.响应url
print(response.url)
# 2.状态码
print(response.status_code)
# 3.响应对应的请求头
print(response.request.headers)
# 4.响应头
print(response.headers)
# 5.请求携带的cookies
print(response.request._cookies())
# 6.响应中携带的cookies
print(response.cookies)
# 7.自动将json字符串类型的响应内容转换为python对象(dict or list)
print(response.json)

打印结果:

三、发送带header的请求头

添加请求头是为了假装成浏览器。
1.在百度网页上面右键,点击“检查”,找到User-Agent,复制。

2.编写代码,将刚才复制的加入到 headers中
没有加header前面的代码打印出来数据只有2287,但是再点开百度的检查,可以知道数据其实不止这么点。加入请求头后数据变多,可以伪装成浏览器。

代码:

import requests
url = 'http://www.baidu'
response = requests.get(url)

print(len(response.content.decode()))
print(response.content.decode())

# 1.构建请求头字典
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'
}

# 2.发送带请求头的请求
response1 = requests.get(url, headers=headers)

# 3.打印出来,对比一下前后数据
print(response1.content)
print(response1.request.headers)

dubug :User-Agent

复制 User-Agent 的时候要把 :单独拿出来,两边用单引号括起来,Mozilla前面不要有空格!!!!!
不然就会出现如下错误:

四、发送带参数的请求

方法一:url中直接带参数

1.查找关键参数

当在网页上面搜索“python”是,百度的网页连接会变成一长串,“?”后面有很多个参数。

https://www.baidu/s?wd=python&rsv_spt=1&rsv_iqid=0xc5896c9000026170&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_dl=tb&rsv_sug3=7&rsv_sug1=8&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&prefixsug=python&rsp=5&inputT=3176&rsv_sug4=3176

关键参数就是删除以后对网页有影响的。
我们就可以从后往前一个个进行删除,知道找到有影响的那一部分 wd=python。

https://www.baidu/s?wd=python

2.编写代码,验证是否可以获取带参数的请求响应

import requests
url = 'https://www.baidu/s?wd=python'
response = requests.get(url)

# 1.构建请求头字典
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'
}

# 2.发送带请求头的请求
response = requests.get(url, headers=headers)

# 3.验证
with open('baidu.html','wb') as f:
    f.write(response.content)

然后就得到了一个baidu.xml 文件,向下划就可以看到已经获取到的内容。

方法二:使用params参数

1.构建参数字典
2.向接口发送请求的时候带上字典,将参数字典设置给params。

代码:
在方法一的代码上变动:url只留下接口url,不要关键参数 wd=python。

import requests
url = 'https://www.baidu/s?'
response = requests.get(url)
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'
}

# 构建参数字典
data={
    'wd':'python'
}

response = requests.get(url, headers=headers,params=data)

print(response.url)
with open('baidu.html','wb') as f:
    f.write(response.content)

再查看一下xml文件:title也出现了python

五、在headers中设置cookies参数

使用GitHub,先登陆GitHub,然后复制自己的账号对应的网址

import requests
url = 'https://github/这里改成自己的用户名'

#构建请求头
headers ={
    'User-Agent':'这里输入检查中的User-Agent',
    'Cookie':'这里输入检查中的cookie'
}

response = requests.get(url,headers=headers)

#第一遍跑代码(无cookie)
with open("github_without_cookies_.html","wb")as f:
    f.write(response.content)
    
#第二遍跑代码(有cookie)
with open("github_with_cookies_.html","wb")as f:
    f.write(response.content)

我自己忘记密码登陆不了github,所以使用了一下csdn实验一下。
没有添加cookies:

添加了cookies:

六、使用Cookies保持会话

1.构建cookies字典
2.在请求时将cookies字典赋值给cookies参数
request,get(url,cookies)

import requests
url = 'https://github/这里改成自己的用户名'

#构建请求头
headers ={
    'User-Agent':'这里输入检查中的User-Agent'
}

#构建cookie字典
temp='这里输入检查中的cookie'

#稳妥分割方案
cookie_list =temp.split(';')
cookies = {}

for cookie in cookie_list:
    cookies[cookie.split('=')[0]] = cookie.split('=')[-1]

#打印出来看一下字典
print(cookies)

接下来继续在最后加上使用cookies的代码:

response = requests.get(url, headers = headers, cookies = cookies)

with open("github_without_cookies_.html2","wb")as f:
    f.write(response.content)

上述代码还可以进行优化

(将上图部分改成下面的字典推导式

最后跑一下代码,在xml文件里面看一下title是否处于登陆状态。

七、CookieJar的对象与Cookie字典的互换

使用request获取的response对象,都有cookies属性,该属性是cookieJar类型的。
CookieJar的对象与Cookie字典的互换的代码如下:

import requests

url = 'http://www.baidu'

response = requests.get(url)

print(response.cookies)

#jar转换为字典
dict_cookies=requests.utils.dict_from_cookiejar(response.cookies)
print(dict_cookies)

#字典转换为jar
jar_cookies=requests.utils.cookiejar_from_dict(dict_cookies)
print(jar_cookies)

运行效果:

八、超时参数timeout的使用

平时网上冲浪会遇到网络波动的情况,一个请求等很久都没有结果。这时就要对请求进行强制要求,让它在特定时间内返回,否则就报错。(默认等待时间是180s,但是有点长,一般手动设置成几秒钟吧)

import requests

url = 'https://twitter'

#3秒找不到结果就报错,但是一般实际情况会大于3秒
response = requests.get(url,timeout=3)

九、代理proxies使用

1.什么是代理?
代理 ip 就是 ip 地址,指向一个代理服务器(代理服务器是用来转发请求的)。
2.代理的分类

(1)正向代理:浏览器知道最终处理请求的服务器的 ip 地址。例如VPN。
(2)反向代理:浏览器不知道最终处理请求的服务器的 ip 地址,最终的请求由代理转发。如nginx。

再根据匿名程度分类:
(1)透明代理:目标服务器知道你(浏览器ip)是谁。

REMOTE _ADDR = Proxy IP
HTTP_VIA = Proxy IP
HTTP__XFORWARDED_FOR = Your IP

(2)匿名代理:别人只知道你用了代理,但是不知道你是谁。

REMOTE_ADDR = proxy IP
HTTP_VIA = proxy IP
HTTP_X_FORWARDED_FOR = proxy IP

(3)高匿代理:别人根本无法发现你是在用代理。

REMOTE_ADDR = Proxy IP
HTTP_VIA = not determined
HTTP_X_FORWARDED_FOR = not determined

当使用的协议不同,对应的代理不同。
http
https
socks
(对方如果使用http,一般情况下不包含https;但是如果对方使用https,可能也可以使用http)

3.用法

response = requests.get(url,proxies=proxies)

代码:(在网上随意搜索一些免费的 ip地址,比如米扑代理,找国内的ip)

import requests
url = 'http://baidu'

#字典有固定格式:(键值对)
#"使用哪种协议":"ip地址"
proxies={
    "http":"http://121.13.252.58",
    # 'https':'https://218.95.81.51.9000',
}

response = requests.get(url,proxies=proxies)
print(response.text)

运行结果:

十、遇到的小问题

1.如何调整pycharm背景

File > Settings > Editor > Color Scheme > Console Colors > Scheme ,选择想要的颜色。

2. pycharm自动换行

对所有文件有效

对控制台自动换行

3.pycharm显示行数

4.github上不去

方法一:github改成hub.fastgit
(这个方法确实可以进入官网了,但是重置密码的页面还是挂)

方法二:修改host文件
(网上很多推荐这个的,貌似在最后还要在cmd窗口写入一句话,但是这个方法我未尝试成功)
句子来源点这里

ipconfig/flushdns

又忘记密码了,反复重置不了,真的上火了!!!🤬
今天学习笔记就暂时写到这吧。

本文标签: 爬虫学习笔记入门day