python爬虫学习(一):requests第一血

前言

最近对爬虫比较感兴趣,在学习python之余也抽空学习,毕竟等自己老了之后也可以靠爬虫搞兼职。当然了,也看看机会能否转行爬虫吧哈哈。言归正传,开搞!

第一篇程序

需求:爬取搜狗首页的页面数据

# -*- encoding: utf-8 -*-
"""
@File    : requests第一血.py
@Time    : 2022/3/1 22:54
@Author  : simon
@Email   : 294168604@qq.com
@Software: PyCharm
"""

#- 需求:爬取搜狗首页的页面数据
import requests
if __name__ == "__main__":
    #step_1:指定url
    url = 'https://www.sogou.com/'
    #step_2:发起请求
    #get方法会返回一个响应对象
    response = requests.get(url=url)
    #step_3:获取响应数据.text返回的是字符串形式的响应数据
    page_text = response.text
    print(page_text)
    #step_4:持久化存储
    with open('./sogou.html','w',encoding='utf-8') as fp:
        fp.write(page_text)
    print('爬取数据结束!!!')

执行结果

posted @ 2022-03-01 23:02  simon_T  阅读(51)  评论(0)    收藏  举报