一、访问robots协议
1、淘宝网站查看robots协议,将蓝色行改成robots.txt
2、显示的是根目录下的所有文件都不允许爬取
二、DNS域名解析
1、向百度发起一个域名请求,回复的是220.181.38.251
2、网络的本质是通过一个IP地址实现一个请求
三、抓包以及了解基本名词
点击进入检查
1、状态码
表示响应成功
100-199 服务器成功接收部分请求,要求客户端继续提交其余请求才能完成整个处理过程
200~299 表示服务器成功接收请求并已完成整个处理过程。常用200(OK 请求成功)
300~399 为完成请求,客户需进一步细化请求。例如:请求的资源已经移动一个新地址、常用302(所请求的页面已经临时转移至新的url)、307和304(使用缓存资源)
400~499:客户端的请求有错误,常用404(服务器无法找到被请求的页面)、403(服务器拒绝访问,权限不够)
500~599:服务器端出现错误,常用500(请求未完成。服务器遇到不可预知的情况)
2、表示能够接收的内容
*/*表示都能够接收
q = 0.8权重的意思,是所占的比例就越高权重越高
3、接收的编码格式
4、接收的语言
5、连接的方式为长连接
6、携带用户的参数信息
7、主机号(端口号)
8、用户的标识(反爬的措施)
伪装成计算机对网站进行访问
9、 GET从服务器上获取指定页面信息
http向百度发起了一个get请求
wd参数指输入的参数
10、 POST是向服务器提交数据并获取页面信息
通过登录向服务器提交数据
显示数据提交到服务器后响应成功
?login里面找到From Data,里面显示输入的用户名以及加密后的密码
是HTTP里面的post 请求
四、安装requests库
1、Ctrl+R打开cmd
2、
3、在Pycharm中按以下步骤,package里面有requests就是安装成功
五、GET请求传参方式
1、通过url以键值对的形式直接传参
2、 通过params以键值对的形式传参
添加多个键值对要用逗号区分开
# -*- coding :utf - 8 -*-
import requests #导库
#准备起始url1
start_url1 = "http://httpbin.org/get"
params = { //传参到args
"id":"2",
"name":"jiujiu"
}
#通过requests发起网络请求
response = requests.get(url=start_url1,params=params)
print(response) #查看响应
print(response.text) #查看文本信息
六、requests请求
# -*- coding :utf - 8 -*-
import requests #导库
#准备起始url
start_url = "http://httpbin.org/get"
#通过requests发起网络请求
response = requests.get(url=start_url)
print(response) #查看响应
print(response.text) #查看文本信息
1、更改User-Agent
计算机标识不是浏览器,就不给访问。需要将User-Agent改成浏览器才能访问到里面的内容
解决方法:将headers改为浏览器的用户标识
# -*- coding :utf - 8 -*-
import requests #导库
#准备起始url
start_url = "http://httpbin.org/get"
headers = {
"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 Edg/94.0.992.47","X-Amzn-Trace-Id": "Root=1-61693536-691adfa354afa261082c266c"
}
#通过requests发起网络请求
response = requests.get(url=start_url,headers=headers)
print(response) #查看响应
print(response.text) #查看文本信息
七、Post
在网页上通过Post访问时访问不到的
在post中Form表单用data进行传参,传的一般是用户名和密码(尽量不要用params传参)
import requests #导库
#准备起始url1
post_url1 = "http://httpbin.org/post"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 Edg/94.0.992.47","X-Amzn-Trace-Id": "Root=1-61693c24-379ab9130d05623b7a82cbfe"
}
data = { #提交的用户名和密码
"username":"hell",
"passwd":"123456"
}
#通过requests发起网络请求
response1 = requests.post(url=post_url1,headers=headers,data=data) #post的请求
print(response1) #查看响应
print(response1.text) #查看文本信息
八、requests响应
1、如果不带headers访问百度,仅能查看外层的信息获取不到里面的数据
import requests
start_url1 = "https://www.baidu.com/"
response = requests.get(url=start_url1,)
response.encoding = 'utf=8'
print(response.text) #查看响应
通过headers伪装成为浏览器登录才能获取里面静态数据
2、查看信息
print(response) #查看响应
print(type(response.text)) #str
print(response.content) #响应的内容为二进制形式
print(type(response.content)) #查看响应的类型
print(response.request) #requests对象
print(response.status_code) #状态码,和print(response)一样
print(response.request.headers) #查看请求头 User-Agent
print(response.request.url) #响应地址
print(response.encoding)
//cookie
cookies = response.cookies
print(type(cookies))
print(requests.utils.dict_from_cookiejar(cookies)) #更改为字典模式
九、 爬取新浪
1、编码格式的转换
#第一种设置编码格式
response.encoding = 'utf-8'
#第二种默认为utf-8
response.encoding = response.apparent_encoding
#第三种根据内容解码
print(response.content.decode())
2、爬取代码
import requests #导库
start_url1 = "https://www.sina.com.cn/"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 Edg/94.0.992.47","X-Amzn-Trace-Id": "Root=1-61693c24-379ab9130d05623b7a82cbfe"
}
response = requests.get(url=start_url1,headers=headers)
response.encoding = 'utf-8'
print(response.text)
十、 爬取百度关键字
1、爬取百度
import requests #导库
#准备起始url1
keyword = input("请输入你要输入的内容:")
start_url1 = "https://www.baidu.com/s?" #注意这里没有加上wd=!!
kw = { 通过用params进行输入内容拼接
"wd" : keyword
}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 Edg/94.0.992.47","X-Amzn-Trace-Id": "Root=1-61693c24-379ab9130d05623b7a82cbfe"
}
response = requests.get(url=start_url1,headers=headers,params=kw)
response.encoding = 'utf-8'
print(response.text)
2、贴吧关键词放入文件夹中
import requests #导库
import os
keyword = input("请输入你要搜索的内容:")
url = "https://tieba.baidu.com/f?ie=utf-8&kw=%E8%BD%A6&fr=search"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 Edg/94.0.992.47","X-Amzn-Trace-Id": "Root=1-61693c24-379ab9130d05623b7a82cbfe"
}
os_path = os.getcwd() + '/html' + '/'+keyword
if not os.path.exists(os_path): #保存到本地
os.makedirs(os_path)
for count,page in enumerate(range(0,101,50)): #枚举和循环来控制抓取的页面
start_url1 = "https://tieba.baidu.com/f?ie=utf-8&kw=%E8%BD%A6&fr=search".format(keyword,page)
print(start_url1)
response = requests.get(url = start_url1,headers=headers )
with open(f'./html/{keyword}/{keyword}_{count+1}.html','w',encoding='utf-8') as f:
f.write(response.text) #设置写入的文件,放入文件内
print("文件夹加载完毕!")