python爬虫知识(二)——requests模块

本文介绍了Python中的requests模块,用于模拟浏览器发送GET和POST请求。通过设置参数和headers,可以实现对网页的搜索功能,并保存响应内容到本地文件。同时,讨论了User-Agent在爬虫和反爬虫中的作用。
摘要由CSDN通过智能技术生成

1. requests模块:python中原生的基于网络请求的模块。下载pip install requests,如果下载失败,参考https://blog.csdn.net/qq_42231156/article/details/113786757

    1.1 作用:模拟浏览器发送请求。官网:https://pypi.org/project/requests/

    1.2 使用:

import requests
if __name__ == "__main__":
    url = "https://www.xxx.com/web"
    kw=input("输入搜索内容:")
    params = {
        "query":kw
    }
    headers = {
       "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
    }
    filepath=kw+".html"

    res = requests.get(url=url, params=params,headers=headers)  
    page_html = res.text  
    with open(filepath, "w", encoding="utf-8") as fp:
        fp.write(page_html)    
    print("get爬取成功")

    // res = requests.post(url=url, data=params,headers=headers)
    // page_json =res.json() ,如果响应结果是json格式,可以通过响应头content-type: application/json;charset=utf-8 判断是否响应为json格式
    // fp=open(filepath,"w", encoding="utf-8")
    // json.dump(page_json,fp=fp,ensure_ascii=False)
    // print("post爬取成功")
    
    

// 爬虫机制之:UA伪装,即User-Agent伪装。
// 反爬虫机制之:UA检查,即User-Agent检查校验。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值