(一)京东商品页面的爬取
https://item.jd.com/2967929.html
import requests
url = "https://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/dp/B07KVK39GQ"
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搜索关键词提交
百度关键词接口: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.so.com/s",params=kv)
print(r.request.url)
r.raise_for_status()
print(len(r.text))
except:
print("爬取失败")
(四)网络图片的爬取和存储
import requests
import os
url = “https://c-ssl.duitang.com/uploads/item/202003/24/20200324231725_vwqfq.jpg”
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(“文件保存成功”)
else:
print(“文件已保存”)
except:
print(“爬取失败”)
(五)IP地址归属地的自动查询
import requests
url = “https://ipchaxun.com”
try:
r = requests.get(url+’/202.204.80.112/’)
r.raise_for_status()
r.encoding = r.apparent_encoding
print(r.text[-500:1])
except:
print(“爬取失败”) 长时间未响应失败