码迷,mamicode.com
首页 > 其他好文 > 详细

Requests库练习

时间:2019-11-22 13:38:15      阅读:49      评论:0      收藏:0      [点我收藏+]

标签:imp   user   归属地   https   item   京东   gen   raise   ams   

实例一:京东商品页面爬取
import requests
url = "http://item.jd.com/2967929.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("爬取失败")
实例三:百度/360搜索关键字提交
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 = ""
root = "D://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("sucess")
else:
print("cunzai")
except:
print("爬取失败")
实例五:IP地址归属地的自动查询
import requests
url = "http://m.ip138.com/ip.asp?ip="
try:
r = requests.get(url+‘202.204.80.112‘)
r.raise_for_status()
r.encoding = r.apparent_encoding
print(r.text[-500:])
except:
print("爬取失败")

Requests库练习

标签:imp   user   归属地   https   item   京东   gen   raise   ams   

原文地址:https://www.cnblogs.com/lskai/p/11910912.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!