Python网络爬虫信息提取mooc代码实例
2020-03-11 16:53
477 查看
实例一--爬取页面
import requests url="https//itemjd.com/2646846.html" try: r=requests.get(url) r.raise_for_status() r.encoding=r.apparent_encoding print(r.text[:1000]) except: print("爬取失败")
正常页面爬取
实例二--爬取页面
import requests url="https://www.amazon.cn/gp/product/B01M8L5Z3Y" try: kv={'user-agent':'Mozilla/5.0'} r=requests.get(url,headers=kv) r.raise_for_status() r.encoding=r.apparent_encoding print(r.text[1000:2000]) except: print("爬取失败")
对访问用户名有限制,模拟浏览器对网站请求
实例三--爬取搜索引擎
#百度的关键词接口:http://www.baidu.com/s?wd=keyword #360的关键词接口:http://www.so.com/s?q=keyword import requests keyword="python" try: kv={'wd':keyword} r=requests.get("http://www.baidu.com/s",params=kv) print(r.request.url) r.raise_for_status() print(len(r.text)) except: print("爬取失败") -------------------------------------------------- import requests keyword="python" try: kv={'q':keyword} r=requests.get("http://www.so.com/s",params=kv) print(r.request.url) r.raise_for_status() print(len(r.text)) except: print("爬取失败")
实例四--:爬取图片
import requests import os url="http://image.nationalgeographic.com.cn/2017/0211/20170211061910157.jpg" root="F://pics//" 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) with open(path,'wb') as f: f.write(r.content) f.close() print("文件保存成功") else: print("文件已经存在") except: print("爬取失败")
爬取并保存图片
实例五--IP地址归属地查询:
http://m.ip138.com/ip.asp?ip=ipaddress
url="http://www.ip138.com/iplookup.asp?ip=" try: r=requests.get(url+'202.204.80.112'+'&action=2') r.raise_for_status() r.encoding=r.apparent_encoding print(r.text[-500:]) except: print("爬取失败")
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
- python爬虫开发之使用python爬虫库requests,urllib与今日头条搜索功能爬取搜索内容实例
- python爬虫开发之selenium模块详细使用方法与实例全解
- python爬虫开发之PyQuery模块详细使用方法与实例全解
- python爬虫开发之urllib模块详细使用方法与实例全解
- python爬虫开发之Request模块从安装到详细使用方法与实例全解
- Python爬虫程序架构和运行流程原理解析
- python爬虫开发之Beautiful Soup模块从安装到详细使用方法与实例
- Python爬虫实现模拟点击动态页面
- Python反爬虫伪装浏览器进行爬虫
- python 爬虫 实现增量去重和定时爬取实例
- python爬虫库scrapy简单使用实例详解
- 详解python 破解网站反爬虫的两种简单方法
- python爬虫模块URL管理器模块用法解析
- 使用Python爬虫库BeautifulSoup遍历文档树并对标签进行操作详解
- Python爬虫库BeautifulSoup获取对象(标签)名,属性,内容,注释
- Python爬虫库BeautifulSoup的介绍与简单使用实例
- 使用Python爬虫库requests发送表单数据和JSON数据
- python爬虫开发之使用Python爬虫库requests多线程抓取猫眼电影TOP100实例
相关文章推荐
- python3.4学习笔记(十三) 网络爬虫实例代码,使用pyspider抓取多牛投资吧里面的文章信息,抓取政府网新闻内容
- Python网络爬虫与信息提取(二):网络爬虫之提取
- Python网络爬虫与信息提取(三):网络爬虫之实战
- python网络爬虫笔记之信息提取
- [mooc]python网络爬虫与信息提取(实例一)
- Python网络爬虫与信息提取-Day14-(实例)股票数据定向爬虫
- 中国大学MOOC·Python网络爬虫与信息提取(二)——五个实例分析
- Python网络爬虫与信息提取(二):网络爬虫之提取
- Python网络爬虫与信息提取-Day10-(实例)中国大学排名定向爬虫
- Python实现爬虫从网络上下载文档的实例代码
- Python网络爬虫与信息提取(二):网络爬虫之规则
- python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例
- Python网络爬虫与信息提取(三):网络爬虫之实战
- python提取照片坐标信息的实例代码
- Python网络爬虫与信息提取(一):网络爬虫之规则
- python3网络爬虫爬取进日头条代码
- Python 网络爬虫--关于简单的模拟登录实例讲解
- python网络爬虫爬取图片代码
- python爬虫_微信公众号推送信息爬取的实例
- Python新手写出漂亮的爬虫代码2——从json获取信息