简单爬今日头条街拍获取图集

emmmmmm这次练手真的是一波三折…不是爬了半天发现是静态网页就是网页重要内容被隐藏要么就是网页参数的内容进行了加密…最后终于找到了头条街拍可以爬,前面都很顺利…然而本来想要获取每一张图片的url的,找了一下午发现在一个文件中隐藏着然后找了很多之前的大佬做过的东西和其他资料发现这个文件很奇怪,格式发生了变化,而且我现在也不确定它到底属于什么格式,所以先放一放,因为这个耗了太久了,等之后再多学学前端的知识再回来解答

要注意这个源代码中不是每条格式都一样,当时这个出过错会显示找不到然后卡住,json格式会很方便,比如这个:
示例1

以下是完整代码:

import json
import requests
from bs4 import BeautifulSoup
import re

count=1#全局变量,用来统计数据个数

def get_url(url):
    global count
    headers={
        'Referer':'https://www.toutiao.com/search/?keyword=%E8%A1%97%E6%8B%8D',
        "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64)AppleWebKit/537.36(KHTML,like Gecko)Chrome/49.0.2623.221Safari/537.36 SE2.XMetaSr 1.0",
        'Cookie':'t_webid=6586448826511410691; WEATHER_CITY=%E5%8C%97%E4%BA%AC; tt_webid=75438450032; UM_distinctid=1650d56f1be0-0b47caa8fde93d8-4c312b7b-100200-1650d56f1c0bb; CNZZDATA1259612802=637489867-1533522354-https%253A%252F%252Fwww.baidu.com%252F%7C1533537864; csrftoken=a4df8ff9c6d9573a34d7fd64e8de0743; uuid="w:84520596a81b4e6dbe440c6de4487b41"; sso_login_status=1; login_flag=00ee0a71eb53051212499264caa2adad; sessionid=03d5db910d7a79213ff761373c0528c9; uid_tt=dd14b69c6344480d00f220dcff9d9a8d; sid_tt=03d5db910d7a79213ff761373c0528c9; sid_guard="03d5db910d7a79213ff761373c0528c9|1533539042|15552000|Sat\054 02-Feb-2019 07:04:02 GMT"; cp=5B672FA3D0C08E1; __tasessionId=p4f4vhtdz1533541390824'
    }
    response = requests.get(url,headers=headers)
    response = response.text
    data = json.loads(response)
    # print(data)
    if data and 'data' in data.keys():#先判断有没有这样的键值
        for item in data.get('data'):
            title = item.get('title')#图集名字
            share_url = item.get('share_url')#图集地址
            if(str(title) and str(share_url)) != 'None':
                print(str(count)+'.'+str(title)+':')#不是每一条数据里的格式都是一样的,是None的数据为了美观丢掉不要了
                print(article_url)
                file = '今日街拍.txt'
                with open(file, 'a', encoding='utf-8') as f:
                    print('第'+str(count)+'数据正在存储...')
                    f.write(title+':'+article_url+'\n')
                count = count+1

#本来是为了获取单个图片的地址的,等以后再来解答
# def get_every_pic(article_url):
#     headers={
#         'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64)AppleWebKit/537.36(KHTML,like Gecko)Chrome/49.0.2623.221Safari/537.36 SE2.XMetaSr 1.0',
#         'cookie':'tt_webid=75438450032; tt_webid=75438450032; WEATHER_CITY=%E5%8C%97%E4%BA%AC; tt_webid=75438450032; UM_distinctid=1650d56f1be0-0b47caa8fde93d8-4c312b7b-100200-1650d56f1c0bb; CNZZDATA1259612802=637489867-1533522354-https%253A%252F%252Fwww.baidu.com%252F%7C1533602664; csrftoken=a4df8ff9c6d9573a34d7fd64e8de0743; uuid="w:84520596a81b4e6dbe440c6de4487b41"; sso_login_status=1; login_flag=00ee0a71eb53051212499264caa2adad; sessionid=03d5db910d7a79213ff761373c0528c9; uid_tt=dd14b69c6344480d00f220dcff9d9a8d; sid_tt=03d5db910d7a79213ff761373c0528c9; sid_guard="03d5db910d7a79213ff761373c0528c9|1533539042|15552000|Sat\054 02-Feb-2019 07:04:02 GMT"; cp=5B672FA3D0C08E1; __tasessionId=4cv4khf2j1533606344299',
#     }
#     response = requests.get(article_url,headers = headers)
#     if response.status_code == 200:
#         html = response.text

def main():
    i=0
    while i <=100 :#观察到动态加载的过程中其实就是这个offset发生了变化,每次变化20,其实这个还可以再增加,看个人喜好,我只爬了100多条数据
        url='https://www.toutiao.com/search_content/?offset='+str(i)+'&format=json&keyword=街拍&autoload=true&count=20&cur_tab=1&from=search_tab'
        get_url(url)
        i=i+20
    # url = 'https://www.toutiao.com/search_content/?offset=' + str(i) + '&format=json&keyword=街拍&autoload=true&count=20&cur_tab=1&from=search_tab'
    # get_url(url)

main()

部分结果截图:
今日街拍结果

街拍文件

猜你喜欢

转载自blog.csdn.net/weixin_42404145/article/details/81477756
今日推荐