今日头条抓取街拍图片数据集

spider1: 抓取街拍页面的所有入口链接:

1.数据查看到,街拍页面需要的数据集都在data这个集合中,而data是整个数据集字典的一个键,data这个键又包括了一个list,list中是一个个字典。

2. list中包括了是图集的,以及是单个图片显示内容的。

3. 对比list中的数据集,可以发现,单个图片显示内容的是有:single_mode这个键

4.同时查看data的数据集:可以看到键‘url’或‘article_url’所对应的值,是我们需要的url入口链接,这边要注意是否有url这个键

5.爬下来的网址发现有一类多余的,要剔除类似:https开头的,news的,即如果没有group在url里面的都要剔除,这是跑了到offset=180的结果

 

 

# 爬取今日头条街拍的数据。
import json

import os
from hashlib import md5

import pymongo
from bs4 import BeautifulSoup
import requests
import re
from urllib.parse import urlencode
from requests.exceptions import RequestException
from config import *  # 将config中的对象全部引入
from multiprocessing import Pool  # 开启进程池

client = pymongo.MongoClient(MONGO_URL, 27017)
db = client[MONGO_DB]
# spider1:负责抓取,街拍的的所有链接入口
def get_all_url(offset, keyword):
    data = {
        'offset': offset,
        'format': 'json',
        'keyword': keyword,
        'autoload': 'true',
        'count': 20,
        'cur_tab': 1,
        'from': 'search_tab'  # some are gallery
    }  # 请求的字符串参数  query string paramenter
    # 对网页进行响应请求:
    url = 'https://www.toutiao.com/search_content/?' + urlencode(data)  # 对data参数进行网络编码
    all_url = []
    try:
        response = requests.get(url)
        if response.status_code == 200:  # 即能够正常请求网络,我们就对网络url进行抓取
            #print(response.text)  # 看一下数据:可以看到数据:\u7687\u57ce类似这种json数据
            response = json.loads(response.text)  # 将其转化为json对象。
          #  print(response)  # 可以看到数据比较正常,此时里面有我们需要抓取的数据。用BeatifulSoup去解析了一下,发现,json对象不能用它解析
            if response and 'data' in response.keys():
                for data in response['data']:  # data 只是response的一个键。
                    if 'single_mode' not in data.keys() and 'article_url' in data.keys():
                      #  all_url.append(data['url'])  # 这个key使用,但是用生成器,貌似会更好,如果需要储存在mongodb中,这个也不错
                      #  print(data.get('url'))  现在返回没有什么问题了
                        yield data.get('article_url')  # 向外面提供url去爬取
    except RequestException as f:
        print('请求页面失败!')


def url_washing(offset):
    '''
    负责url的清洗工作,最后得到的是需要的url,有用的数据。
    '''
    url = []
    # offset = [x for x in range(Group_start, Group_end, 20)]  # url也是一个迭代器
    for i in offset:
        for j in get_all_url(i, '街拍'):
            if 'group' in j:
                url.append(j)
                #print(j)
    return url


# spider2: 爬取详情页的详细信息
def get_detail(url):
    '''
    url:每一个详情页的url
    :return: 详情页的标题,以及图片网址,一个详情页,图片网址不止一个
    '''
    try:
        response = requests.get(url)
        if response.status_code == 200:  # 即能够正常请求网络,我们就对网络url进行抓取
            soup = BeautifulSoup(response.text, 'lxml')
            title = soup.select('title')[0].get_text()
            images_pattern = re.compile('gallery: JSON.parse\\((.*?)\\)', re.S)
            images = re.search(images_pattern, response.text)
            images = json.loads(images.group(1))  # json.loads 不能转换单引号数据类型,json对象。
            image_url = re.findall('\\"(http.*?)\\"', images)
            image_url1 = []
            for i in image_url:
                image_url1.append(i.replace('\\',''))
            data = {
                'title': title,
                'url': url,
                'image_url': image_url1
            }
            return data  # 每一个详情页的数据
    except RequestException as f:
        print('请求详情页失败!')


def download_image(image_url):
    '''
    :param image_url: 每一个详情页图片的网址
    :return: 将网址中的图片下载到本地
    '''
    try:
        response = requests.get(image_url)
        if response.status_code == 200:  # 即能够正常请求网络,我们就对网络url进行抓取
            sava_image(response.content) # content 是获取图片,视频的二进制内容
    except RequestException as f:
        print('请求图片失败!')

def sava_image(content):

    file_path = '{0}/{1}.{2}'.format(os.getcwd(),md5(content).hexdigest(), 'jpg')
    if not os.path.exists(file_path):
        with open(file_path, 'wb') as f:
            f.write(content)
            f.close()

def sava_to_mongo(data):
    if db[MONGO_TABLE].update(data, data, upsert=True):  # 更新数据库比较好,后面有是否插入的参数
        print('储存到MongoDB成功!', data)
        return True
    return False


def main(offset): # 定义一个主函数,操作所以函数
    # offset = [x for x in range(Group_start, Group_end, 20)]
    url = url_washing(offset)
    for i in url:
        data = get_detail(i)
        sava_to_mongo(data)
        # for url in data['image_url']:
        #     print('正在下载图片',url)
        #     download_image(url)



if __name__ == '__main__':
    group = [x * 20 for x in range(Group_start, Group_end + 1)]  # [x for x in range(Group_start, Group_end, 20)]
    main(group)
    # pool = Pool()
    # pool.map(main, group)  # apply_async(main)
    # pool.close()
    # pool.join()

  

 

posted on 2018-02-01 23:31  java小萌新(づ ̄3 ̄)  阅读(1853)  评论(0编辑  收藏  举报

导航