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('爬取数据完成')
爬取成功



浙公网安备 33010602011771号