[Python/爬虫/]校花网

最近一直在学习 爬虫,感觉模块很多种,比如比较偏向web的xpath(lxml),比较简单的bs4,还有正则的re, 今天爬取了一下 校花网的一个简单案例 代码如下:

#/usr/bin/env python
# -*- coding:utf-8 -*-
from urllib import request
from bs4 import BeautifulSoup
import re
import requests
import urllib

url = 'http://www.xiaohuar.com/list-1-{}.html'
class SchoolFlower(object):
    def __init__(self,url):
        self.url = url
        self.header = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.109 Safari/537.36'}
        self.List = []
        self.Connection()

    def Connection(self):
        content = requests.get(self.url,headers=self.header)
        content.encoding = 'utf-8'
        soup = BeautifulSoup(content.content,'html.parser',from_encoding='gbk')
        return soup

    def Getpicture(self):
        info = self.Connection()
        img = info.find_all('img',src=re.compile(r'/d/file/\d+/\w+\.jpg'))
        x = 0
        for i in img:
            print('正在下载弟{}张照片'.format(x))
            f = open('%s.jpg' % i.get('alt'),'wb')
            f1 = requests.get('http://www.xiaohuar.com%s' % i.get('src')).content
            f.write(f1)
            x += 1
        x = 0

for i in range(0,11):
    print('#########开始下载第{}页的图片##########'.format(i))
    f = SchoolFlower(url.format(i))
    f.Getpicture()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值