Python网络爬虫信息提取mooc代码实例

作者:159566 时间:2022-01-02 12:18:23 

实例一--爬取页面


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("爬取失败")

来源:https://www.cnblogs.com/cy2268540857/p/12424091.html

标签:python,爬虫,信息,mooc
0
投稿

猜你喜欢

  • rs.open sql,conn,1,1全接触

    2007-11-01 22:46:00
  • MYSQL数据库常用命令集合

    2009-02-26 16:01:00
  • 基于OpenCv实现的人脸识别(附Python完整代码)

    2022-08-10 21:49:27
  • JavaScript match() 方法

    2007-11-04 13:28:00
  • php中instanceof 与 is_a()区别分析

    2023-11-19 06:04:56
  • CSS网页布局扩展小技巧

    2010-06-03 12:13:00
  • python获取点击的坐标画图形的方法

    2023-04-23 09:59:28
  • 你是真正的用户体验设计者吗? Ⅴ

    2008-04-19 18:32:00
  • python解析html开发库pyquery使用方法

    2021-08-28 18:18:57
  • python字符串连接方式汇总

    2021-08-19 23:46:14
  • 产品交互原型设计工具分享

    2010-03-24 18:06:00
  • HTML 5 胜出:XHTML2 宣告夭折

    2009-07-12 15:23:00
  • Python中的变量及简单数据类型应用

    2021-06-13 09:45:26
  • ASP强制刷新和判断文件地址是否存在

    2007-09-16 17:11:00
  • 禁止背景图在网页中平铺

    2011-04-29 14:10:00
  • ie和火狐兼容问题

    2010-07-02 12:50:00
  • 如何正确显示模式对话框showModalDialog中的中文?

    2010-06-28 18:24:00
  • wap开发 完整的WML文档结构详解

    2008-05-21 13:39:00
  • 分析并输出Python代码依赖的库的实现代码

    2022-04-05 17:07:23
  • ASP的错误代码都有哪些?

    2009-10-28 18:15:00
  • asp之家 网络编程 m.aspxhome.com