前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >爬取博客园的所有随笔的url以及计数,还有对应标题

爬取博客园的所有随笔的url以及计数,还有对应标题

作者头像
小小咸鱼YwY
发布2019-07-24 16:36:12
3430
发布2019-07-24 16:36:12
举报
文章被收录于专栏:python-爬虫

1.爬取博客园的所有随笔的url以及计数,还有对应标题

代码语言:javascript
复制
import re
import requests
from lxml.html import etree
import json

#对于链接和标题的一个整合
def func_1_deco(func_1):
    def wrapper(*args,**kwargs):
        dic = dict()
        lis = func_1(*args,**kwargs)
        count = lis[0]
        url_lis = lis[1]
        dic['count'] = count
        name_xpath = '//*[@id="cb_post_title_url"]/text()'
        for url in url_lis:
            response = requests.get(url)
            response = response.text
            response_html = etree.HTML(response)
            name = response_html.xpath(name_xpath)[0]
            print(name)
            dic[name] = url
        return dic
    return wrapper

@func_1_deco
def func(url):
    lis = []
    count = 1
    while True:
        count_1 = len(lis)
        response = requests.get(f'{url}default.html?page={count}')
        response = response.text
        data_1 = re.findall(' href="(.*?)"', response, re.S)
        for a in data_1:  # type:str
            if a.startswith('http'):
                if a.endswith('html'):
                    if 'archive' not in a:
                        lis.append(a)
        count +=1
        lis = set(lis)
        lis = list(lis)
        count_2 = len(lis)

        if count_1 == count_2:
            return count_2,lis  #博客的数据量,博客里面随笔的url

        
dic = func('你的博客的首页地址') #注意结尾要有/,字典格式是有一栏'count'计数,其他均为标题+对应的url
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-06-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.爬取博客园的所有随笔的url以及计数,还有对应标题
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档