python学习

所花时间(包括上课):

 2h

代码量(行):

 150左右

搏客量(篇):

 1

了解到的知识点:

python

备注(其他):

 

requests使用步骤

  • 指定URL
  • 发起请求
  • 获取响应数据
  • 持久化存储

1、安装requests

在pythonCharm中下载requests软件包

 2、代码练习——爬取搜狗首页页面数据

复制代码
# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':
    #1、指定url
    url = 'https://www.sogou.com/'
    #2、发起请求
    #get方法会返回一个响应对象
    response = requests.get(url=url)
    #3、获取响应数据.text返回的是字符串形式的响应数据
    page_text = response.text
    print(page_text)
    #4、持久化存储
    with open('./sougou.html', 'w', encoding='utf-8') as fp:
        fp.write(page_text)
    print('爬取数据完成')
复制代码

爬取成功

posted @ 2024-06-19 17:21  平安喜乐×  阅读(17)  评论(0)    收藏  举报