先写一下爬虫的基础框架,后面有时间在慢慢总结补充完善。
爬虫简单示例—爬取百度网页图片
#爬取百度网页图片
# 导入网络请求模块(该模块需要使用pip install requests 安装)
import requests
# 创建请求头
headers = {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36'
}
# 发送网络请求
response = requests.get(url='https://www.baidu.com/img/bd_logo1.png?where=super',headers = headers)
#将获取的二进制内容进行保存
# f=open('./baidu.png','wb')
# f.write(response.content)
# f.close()
with open('./baidu.png','wb') as f: #不需要写关闭文件的操作
f.write(response.content)
import requests
# 创建请求头
headers = {
"User-Agent":'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/74.0.3729.131 Safari/537.36'
}
# 发送网络请求
response = requests.get(url='https://www.toutiao.com/',headers = headers)
print(response.text)