实战1:爬取京东商品信息
# -*- coding: UTF-8 -*-
import requests
def getJD(url):
try:
kv = {'user-agent': 'Mozilla/5.0'}
r = requests.get(url, headers=kv, timeout=30)
r.raise_for_status() # 若状态不是200,引发HTTPError异常
r.encoding = r.apparent_encoding
return r.text
except:
return '产生异常'
if __name__ == '__main__':
url = "https://item.jd.hk/41495598488.html"
getJD(url)
实战2:爬取网络图片并存储
# -*- coding: UTF-8 -*-
import requests
import os
def getPicture(url, root):
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()
return '文件保存成功'
else:
return '文件已存在'
except:
return '爬取失败'
if __name__ == '__main__':
url = "http://www.baidu.com/...jpg"
root = 'C://picture//'
getPicture(url, root)
实战3:自动查询IP地址的归属地
# -*- coding: UTF-8 -*-
import requests
# ip地址归属的自动查询
def getIpAddress(url, ip):
try:
kv = {'user-agent': 'Mozilla/5.0'}
r = requests.get(url + ip)
r.raise_for_status() # 若状态不是200,引发HTTPError异常
r.encoding = r.apparent_encoding
return r.text[-500:]
except:
return '爬取失败'
if __name__ == '__main__':
url = "https://m.ip138.com/iplookup.asp?ip="
ip = "192.168.31.49"
getIpAddress(url, ip)