一、模拟登陆
- 使用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)
response = requests.get(url, verify=False)
response = requests.get(url, timeout=3)
proxy = {
'http': '125.108.108.165: 9000'
}
response = requests.get(url, proxies=proxy)
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')
from urllib.parse import urljoin
url = urljoin('https://www.dy2018.com/html/gndy/dyzz/index.html', '/i/102048.html')
三、数据提取
json.dumps()
json.loads()
json.dump()
json.load()
import jsonpath
jsonpath.jsonpath()
JsonPath | 描述 |
---|
$ | 根节点 |
@ | 现行节点 |
. or [] | 取子节点 |
n/a | 取父节点,JsonPath不支持 |
… | 当前节点下,不管位置,选择所有符合条件的 |
* | 匹配所有元素节点 |
n/a | 根据属性访问,json不支持 |
[] | 迭代器标示 |
[,] | 支持迭代器中做多选 |
?() | 支持过滤操作 |
() | 支持表达式计算 |
n/a | 分组,JsonPath不支持 |