python网络爬虫五个小实例

实例一

爬取京东商品信息

import requests
url="https://item.jd.com/6946605.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/B07D51Y714"
try:
    kv={'user-agent':'Mozailla/5.0'}
    r=requests.get(url,headers=kv)
    r.raise_for_status()
    r.encoding=r.apparent_encoding
    print(r.text[1000:2000])
except:
    print("爬取失败")

实例三

百度搜索

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
import os
url="http://img0.dili360.com/ga/M00/4A/77/wKgBzFsfM-2ADQ9iAC7rBKt1uIE377.tub.jpg"
root="E:/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="http://www.ip138.com/ips138.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("爬取失败")

 

你可能感兴趣的:(学习,笔记,python,网络爬虫)