爬虫入门(11)——cnblog博客的文章内容爬取

本文介绍了如何使用Python爬取CNBlog博客文章内容,通过分析页面参数构造URL,解决只获取到第一页数据的问题。探讨了多线程和多进程在爬虫中的应用,并提供了相关参考资料。
摘要由CSDN通过智能技术生成

1.前言

根据页面链接:https://www.cnblogs.com/#p4
修改p4为p1-200,都可以获得相应的页面的链接,但是,如果写代码也是这样写,就不行了,只会获得p1页面的各篇博客的链接。
代码如下:

# coding: utf-8
# Author: shelley
# 2020/9/21,9:57

import requests
import re

# 会得到相同的urls,出错。

def get_articles():
    url = 'https://www.cnblogs.com/#p'
    headers = {
   
        'Referer': 'https://www.cnblogs.com/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
    }
    for i in range(1, 200+1,1):

        url_=url.format(i)

        text = requests.get(url_, headers=headers).text
        text = text.replace('\xbb', '')

        pattern = '<a class="post-item-title" href="https://www.cnblogs.com/.*?/p/.*?.html" target="_blank">'
        articles = re.findall(pattern, text)
        for article in articles:
            pat = 'https://www.cnblogs.com/.*?/p/.*?.html'
            article = re.findall(pat, article)
            print(article[0])
            # text = requests.get(article[0]).text


if __name__ == '__main__':
    get_articles()

2.页面分析

打开首页,进入审查页面的network,进行分析,在AggSitePostList里,可以看到参数的请求,Request Payload,

CategoryId: 808
CategoryType: “SiteHome”
ItemListActionName: “AggSitePostList”
PageIndex: 4
ParentCategoryId: 0
TotalPostCount: 4000

把请求参数提取出来,构造URL,访问第15页列表页:
http://www.cnblogs.com/?CategoryId=808&CategoryType=%22SiteHome%22&ItemListActionName=%22AggSitePostList%22&PageIndex=4&ParentCategoryId=0

url的?表示参数请求,&表示多个参数,字符串参数需要用%22包含起来。
用这个链接在浏览器的显示也是首页,不是其他的页面。写代码测试下。结果获得的还是首页的20个博客的链接。

在这里插入图片描述
暂时没想到方法,不过看到别人的headers写了很多东西,就把AggSitePostList里的headers可以加的加进入,果然有效!!!哈哈哈哈哈哈哈。

代码如下:

# coding: utf-8
# Author: shelley
# 2020/9/21,9:57

import requests
import re

# 会得到相同的urls,出错。

def get_articles():
    # url = 'https://www.cnblogs.com/#p{}'
    url = 'http://www.cnblogs.com/?CategoryId=808&CategoryType=%22SiteHome%22&ItemListActionName=%22AggSitePostList%22&PageIndex={}&ParentCategoryId=0'
    headers = {
   
        'Referer': '
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值