爬虫实例

 


目录

 

1. 京东商品页面的爬取 

2. 亚马逊商品页面的爬取 

3. 百度与360搜素关键词提交

4. 图片爬取


慕课课程学习笔记

1. 京东商品页面的爬取 

对象:https://item.jd.com/100000947807.html

1.采用get()方法,获取Response对象;

import requests

url = 'https://item.jd.com/100000947807.html'
r = requests.get(url)
print(r.status_code)

                                                                      

r.status_code状态码为200,获取成功。

2.采用encoding,查看编码

print(r.encoding)

结果:gbk

说明从页面的头部分就可以解析页面的编码信息

全代码

import requests
url = "https://item.jd.com/100000304401.html"
try:
    r = requests.get(url)
    r.raise_for_status()
    r.encoding = r.apparent_encoding
    print(r.text[:1000])
except:
    print("爬取失败")

其中  r.raise_for_status()在方法内部判断r.status_code是否等于200,不需要增加额外的if语句,该语句便于利用try‐except进行异常处理

 

2. 亚马逊商品页面的爬取 

对象:https://www.amazon.cn/gp/product/B01M8L5Z3Y

第一步,状态码为503,访问出现错误


url = "https://www.amazon.cn/gp/product/B01M8L5Z3Y"
r  = requests.get(url)
print(r.status_code)

第二步,查看编码,改变编码

print(r.encoding)
r.encoding = r.apparent_encoding#改成可以阅读的相关编码,utf-8
print(r.encoding)
r.text

                                                                                        

网站对爬虫限制

robot协议

对网站访问的头,来查看是不是爬虫

第三步,查看头

print(r.request.headers)

                                                            

在request中,告诉访问来自用request库;因此需要模拟浏览器访问

第四步,模拟浏览器访问

kv = {'uers-agent':'Mozilla/5.0'}

Mozilla/5.0:标准的浏览器的身份表示字段

kv = {'uers-agent':'Mozilla/5.0'}
r = requests.get(url,headers=kv)

查看状态访问成功;

全代码:

url = "https://www.amazon.cn/dp/B07GVXHCXH/ref=zg_bsnr_books_1?_encoding=UTF8&psc=1&refRID=R1EE4EWH03FPE6V7K9HY"
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('爬取失败')

3. 百度与360搜素关键词提交

搜索关键词接口

http://www.baidu.com/s?wd=Python

全代码:

keyword = "Python"
try:
    kv = {'wd':keyword}#kv = {'q':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("爬取失败")

4. 图片爬取

连接:http://image.nationalgeographic.com.cn/2015/0121/20150121033625957.jpg

全代码:

import os

url = 'http://image.nationalgeographic.com.cn/2015/0121/20150121033625957.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)#content处理二进制
            f.close()
            print("文件保存成功")
    else:
        print("文件已经存在")
except:
    print("爬取失败")

IP地址归属地的自动查询

ip138:http://m.ip138.com/ip.asp?ip=

全代码:

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("爬取失败")

网站人机交互形式,是通过链接提交的,则可以通过python模拟向后台服务器提交的

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值