Python requests库使用实例

来源:互联网 发布:mysql多表 union 用法 编辑:程序博客网 时间:2024/04/25 20:26

1、简单网络爬虫的通用框架

import requestsdef getHTML(url):try:r = requests.get(url)r.raise_for_status() #如果返回的状态不是200,引发HTTPError异常r.encoding = r.apparent_encodingreturn r.textexcept:return "产生异常"if __name__ == "__main__":url="http://www.baidu.com"print(getHTML(url))
2、使用request下载网上的图片

import requestsimport osroot = "e://";url = "http://image.nationalgeographic.com.cn/2017/0316/20170316025141554.jpg";path = root + url.split("/")[-1]try:if not os.path.exists(root):os.mkdir(root);if not os.path.exists(path):r = requests.get(url)r.raise_for_statuswith open(path,'wb') as f:f.write(r.content)f.close()except:print("爬取失败")

-----中国大学MOOC嵩天老师的听课笔记

1 0
原创粉丝点击