爬虫-request模块

request模块

1、基本操作

'''
    - 指定url
    - 发起请求
    - 获取响应的数据
    - 持久化存储
'''
import requests
if __name__ == "__main__":
    # 指定url
    url = "https://www.sogou.com/"
    # 发送请求
    response = requests.get(url=url)
    # 获取相应数据,text返回的是字符串类型的相应数据
    page_text = response.text
    print(page_text)
    # 持久化存储
    with open("./sougou.html", "w", encoding="UTF-8") as fp:
        fp.write(page_text)

2、UA伪装

# UA:user-agent
# UA伪装
import requests
if __name__ == "__main__":
    url = "https://www.sogou.com/web"

    kw = input('enter a word:')
    # UA伪装
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0'
    }
    # 字典,相当于url的请求里的参数
    param = {
        'query': kw
    }
    # 代理,正常用https和sockets5
    proxies = {"HTTP":"http://123.169.122.201:9999"}
    response = requests.get(url=url, params=param, headers=headers, proxies=proxies)
    page_text = response.text
    with open("./sougou.html", "w", encoding="UTF-8") as fp:
        fp.write(page_text)

3、小案例

# 整张页面的局部数据
# 破解百度翻译
'''
- poat请求(携带了参数)
- 相应的数据是一组json数据
json.load('json数据')可以变成字符串形式
'''

import requests
import json
if __name__ == "__main__":
    post_url = "https://fanyi.baidu.com/sug"
    # post请求的参数
    data = {
        'kw': 'dog'
    }
    #UA伪装
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0'
    }
    response = requests.post(url=post_url, data=data, headers=headers)
    # 返回的是一个json对象(相应的数据必须为json数据)
    dic_obj = post_text = response.json()
    # print(dic_obj)
    # 存储
    with open('./dog.json', 'w', encoding='UTF-8') as fp:
        json.dump(dic_obj, fp=fp, ensure_ascii=False, indent=2)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

horizonTel

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值