网络爬出五个小实例

实例一

爬取京东商品信息


  
  
  1. import requests
  2. url= "https://item.jd.com/6946605.html"
  3. try:
  4. r=requests.get(url)
  5. r.raise_for_status()
  6. r.encoding=r.apparent_encoding
  7. print(r.text[: 1000])
  8. except:
  9. print( "爬取失败")


实例二

爬取亚马逊商品信息


 
 
  1. import requests
  2. url= "https://www.amazon.cn/gp/product/B07D51Y714"
  3. try:
  4. kv={ 'user-agent': 'Mozailla/5.0'}
  5. r=requests.get(url,headers=kv)
  6. r.raise_for_status()
  7. r.encoding=r.apparent_encoding
  8. print(r.text[ 1000: 2000])
  9. except:
  10. print( "爬取失败")

实例三

百度搜索


 
 
  1. import requests
  2. keyword= "Python"
  3. try:
  4. kv={ 'wd':keyword}
  5. r=requests.get( "http://www.baidu.com/s",params=kv)
  6. print(r.request.url)
  7. r.raise_for_status()
  8. print(len(r,text))
  9. except:
  10. print( "爬取失败")

实例四

爬取单张网络图片


 
 
  1. import requests
  2. import os
  3. url= "http://img0.dili360.com/ga/M00/4A/77/wKgBzFsfM-2ADQ9iAC7rBKt1uIE377.tub.jpg"
  4. root= "E:/pics//"
  5. path=root +url.split( '/')[ -1]
  6. try:
  7. if not os.path.exists(root):
  8. os.mkdir(root)
  9. if not os.path.exists(path):
  10. r=requests.get(url)
  11. with open(path, 'wb') as f:
  12. f.write(r.content)
  13. f.close()
  14. print( "文件保存成功")
  15. else:
  16. print( "文件已存在")
  17. except:
  18. print( "爬取失败")

实例五

IP地址查询


 
 
  1. import requests
  2. url= "http://www.ip138.com/ips138.asp?ip="
  3. try:
  4. r=requests.get(url+ '202.204.80.112')
  5. r.raise_for_status()
  6. r.encoding=r.apparent_encoding
  7. print(r.text[ -500:])
  8. except:
  9. print( "爬取失败")

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值