<()参数
查询参数-params
1.参数类型
  字典,字典中键值对作为查询参数
2.使⽤⽅法
1、res = (url,params=params,headers=headers)
2、特点:
* url为基准的url地址,不包含查询参数
* 该⽅法会⾃动对params字典编码,然后和url拼接
3.⽰例
import requests
baseurl = 'tieba.baidu/f?'
params = {
'kw' : '赵丽颖吧',
'pn' : '50'
}
headers = {'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3) # ⾃动对params进⾏编码,然后⾃动和url进⾏拼接,去发请求
res = (baseurl,params=params,headers=headers)
)
web客户端验证参数-auth
1.作⽤类型
1、针对于需要web客户端⽤户名密码认证的⽹站
2、auth = ('username','password')
2.通过⽤户名账号密码获取笔记名称案例
import requests
from lxml import etree
import os
class NoteSpider(object):
def__init__(self):
self.url = 'code/Code/aid1904/redis/'
self.headers = {'User-Agent':'Mozilla/5.0'}
self.auth = ('code','code_2013')
# 获取
def get_html(self):
html = (url=self.url,auth=self.auth,headers=self.headers).text
return html
# 解析提取数据 + 把笔记压缩包下载完成
def parse_page(self):
html = _html()
xpath_bds = '//a/@href'
parse_html = etree.HTML(html)
# r_list : ['../','day01','day02','redis_day01.zip']
r_list = parse_html.xpath(xpath_bds)
for r in r_list:
dswith('zip') dswith('rar'):
print(r)
if__name__ == '__main__':
spider = NoteSpider()
spider.parse_page()
思考:爬取具体的笔记⽂件?
import requests
from lxml import etree
import os
class NoteSpider(object):
def__init__(self):
self.url = 'code/Code/redis/'
self.headers = {'User-Agent':'Mozilla/5.0'}
self.auth = ('code','code_2013')
# 获取
def get_html(self):
html = (url=self.url,auth=self.auth,headers=self.headers).text
return html
# 解析提取数据 + 把笔记压缩包下载完成
def parse_page(self):
html = _html()
xpath_bds = '//a/@href'
parse_html = etree.HTML(html)
# r_list : ['../','day01','day02','redis_day01.zip']
r_list = parse_html.xpath(xpath_bds)
for r in r_list:
dswith('zip') dswith('rar'):
file_url = self.url + r
self.save_files(file_url,r)
def save_files(self,file_url,r):
html_content = (file_url,headers=self.headers,auth=self.auth).content
# 判断保存路径是否存在
directory = '/home/redis/'
filename = directory + r
    #适⽤频率很⾼
    #if not ists('路径'):
    #  os.makedirs('路径') 可递归创建
    #  os.mkdir('路径')不能递归创建
if not ists(directory):
os.makedirs(directory)
with open(filename,'wb') as f:
f.write(html_content)
print(r,'下载成功')
if__name__ == '__main__':
spider = NoteSpider()
spider.parse_page()
SSL证书认证参数-verify
1.适⽤⽹站及场景
1、适⽤⽹站: https类型⽹站但是没有经过证书认证机构认证的⽹站
2、适⽤场景: 抛出 SSLError 异常则考虑使⽤此参数
2.参数类型
1、verify=True(默认)  : 检查证书认证
2、verify=False(常⽤): 忽略证书认证
# ⽰例
response = (
url=url,
params=params,
headers=headers,
verify=False
)
代理参数-proxies
1.定义
1、定义: 代替你原来的IP地址去对接⽹络的IP地址。
2、作⽤: 隐藏⾃⾝真实IP,避免被封。
2.普通代理
  获取代理IP⽹站
西刺代理、快代理、全⽹代理、代理精灵、... ...
  参数类型
1、语法结构
proxies = {
'协议':'协议://IP:端⼝号'
}
2、⽰例
proxies = {
'http':'IP:端⼝号',
'https':'IP:端⼝号'
}
  ⽰例代码
    (1)使⽤免费普通代理IP访问测试⽹站:
import requests
url = '/get'
headers = {
'User-Agent':'Mozilla/5.0'
}
# 定义代理,在代理IP⽹站中查免费代理IP
proxies = {
'http':'112.85.164.220:9999',
'https':'112.85.164.220:9999'
}
html = (url,proxies=proxies,headers=headers,timeout=5).text
print(html)
    思考: 建⽴⼀个⾃⼰的代理IP池,随时更新⽤来抓取⽹站数据
1.从代理IP⽹站上,抓取免费的代理IP
2.测试抓取的IP,可⽤的保存在⽂件中
    (2)写⼀个获取收费开放代理的接⼝
# 获取开放代理的接⼝
import requests
def test_ip(ip):
url = 'www.baidu/'
proxies = {
'http':'{}'.format(ip),
'https':'{}'.format(ip),
}
try:
res = (url=url,proxies=proxies,timeout=8)
if res.status_code == 200:
return True
except Exception as e:
return False
# 提取代理IP
def get_ip_list():
api_url = 'dev.kdlapi/api/getproxy/?orderid=946562662041898&num=100&protocol=1&method=2&an_an=1&an_ha=1&sep=2'  html = (api_url).content.decode('utf-8','ignore')
ip_port_list = html.split('\n')
for ip in ip_port_list:
with open('','a') as f:
if test_ip(ip):
f.write(ip + '\n')
if__name__ == '__main__':
get_ip_list()
实现代码
    (3)使⽤随机收费开放代理IP写爬⾍
import random
import requests
class BaiduSpider(object):
def__init__(self):
self.url = 'www.baidu/'
self.headers = {'User-Agent' : 'Mozilla/5.0'}
self.blag = 1
def get_proxies(self):
with open('','r') as f:
xpath语法 python
#f.readlines:['1.1.1.1:111\n','2.2.2.2:22\n']
result = f.readlines()
#[:-1] -> 切掉ip,port后的\n
proxy_ip = random.choice(result)[:-1]
proxy_ip = {
'http':'{}'.format(proxy_ip),
'https': '{}'.format(proxy_ip)
}
return proxy_ip
def get_html(self):
proxies = _proxies()
if self.blag <= 3:
try:
html = (url=self.url,proxies=proxies,headers=self.headers,timeout=5).text print(html)
except Exception as e:
print('Retry')
self.blag += 1
<_html()
if__name__ == '__main__':
spider = BaiduSpider()
<_html()
实现代码
3.私密代理
  语法格式
1、语法结构
proxies = {
'协议':'协议://⽤户名:密码@IP:端⼝号'
}
2、⽰例
proxies = {
'http':'⽤户名:密码@IP:端⼝号',
'https':'⽤户名:密码@IP:端⼝号'
}
  ⽰例代码
import requests
url = '/get'
proxies = {
'http': '309435365:szayclhp@106.75.71.140:16816',
'https':'309435365:szayclhp@106.75.71.140:16816',
}
headers = {
'User-Agent' : 'Mozilla/5.0',
}
html = (url,proxies=proxies,headers=headers,timeout=5).text
print(html)
urllib和urllib2关系
#python2
urllib :URL地址编码
urllib2:请求
#python3 - 把python2中urllib和urllib2合并
urllib.parse:编码
控制台抓包
打开⽅式⼏常⽤选项
1、打开浏览器,F12打开控制台,到Network选项卡
2、控制台常⽤选项
1、Network: 抓取⽹络数据包
1、ALL: 抓取所有的⽹络数据包
2、XHR:抓取异步加载的⽹络数据包
3、JS : 抓取所有的JS⽂件
2、Sources: 格式化输出并打断点调试JavaScript代码,助于分析爬⾍中⼀些参数
3、Console: 交互模式,可对JavaScript中的代码进⾏测试
3、抓取具体⽹络数据包后
1、单击左侧⽹络数据包地址,进⼊数据包详情,查看右侧
2、右侧:
1、Headers: 整个请求信息
General、Response Headers、Request Headers、Query String、Form Data
2、Preview: 对响应内容进⾏预览
3、Response:响应内容

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。