爬虫补充笔记(一)

一、模拟登陆
  • 使用Session对象发送post请求
  • 直接找登录接口携带cookie登录
  • 使用selenium模拟浏览器登录
二、requests参数注意事项
  • 在指定cookies参数时需传入cookiejar对象
    dict与cookiejar对象相互转换
from requests.utils import dict_from_cookiejar, cookiejar_from_dict
response = requests.get(url, cookies=cookies)
  • 处理证书错误(SSLError)
response = requests.get(url, verify=False)
  • 超时处理
response = requests.get(url, timeout=3)
  • 代理IP
proxy = {
	'http': '125.108.108.165: 9000'
}
response = requests.get(url, proxies=proxy)
  • retrying模块
import requests
from retrying import retry

@retry(stop_max_attempt_number=3)
def get_response(url):
	return requests.get(url, timeout=2) 
  • 编码解析错误
# 忽略编码错误
requests.get(url).content.decode(utf-8, errors='ignore')
  • url拼接
# 1. 手动拼接
# 2. 使用模块拼接
from urllib.parse import urljoin
url = urljoin('https://www.dy2018.com/html/gndy/dyzz/index.html', '/i/102048.html')
三、数据提取
  • json数据处理
json.dumps() # 将字典转为字符串
json.loads() # 将字符串转为字典

json.dump() # 操作类文件对象,向文件中写入并将字典转为字符串
json.load() # 操作类文件对象,从文件中读取并将字符串转为字典
  • jsonpath的使用
import jsonpath
jsonpath.jsonpath()
  • JsonPath语法
JsonPath描述
$根节点
@现行节点
. or []取子节点
n/a取父节点,JsonPath不支持
当前节点下,不管位置,选择所有符合条件的
*匹配所有元素节点
n/a根据属性访问,json不支持
[]迭代器标示
[,]支持迭代器中做多选
?()支持过滤操作
()支持表达式计算
n/a分组,JsonPath不支持
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值