【Day 13】- 用 JSON 储存爬来的 PTT 文章。(实战 PTT 爬虫 3/3)

前情提要

前一篇文章带大家写了能爬取持续爬取 PTT 文章的爬虫。

开始之前

本篇将继续带各位写 PTT 爬虫,今天会将爬取到的文章内容用 JSON 档案储存起来。

预期效果

将爬取到的文章内容储存於 JSON 档案中。

实作

我们先定义一个存放所有文章资讯的串列

article_list = []

之後,我们将每一篇文章资讯存为一个字典,并将这个字典加入存放所有文章资讯的串列内。

title = art.find('div', class_='title').getText().strip()
if not title.startswith('(本文已被删除)'):
    link = 'https://www.ptt.cc' + \
        art.find('div', class_='title').a['href'].strip()
author = art.find('div', class_='author').getText().strip()
article = {
    'title': title,
    'link': link,
    'author': author
}

之後读者可以将存放所有文章资讯的串列输出看是否正常,这边统整一下目前的程序码。

import requests
from bs4 import BeautifulSoup

article_list = []

def get_resp(url):
    cookies = {
        'over18': '1'
    }
    resp = requests.get(url, cookies=cookies)
    if resp.status_code != 200:
        return 'error'
    else:
        return resp

def get_articles(resp):
    soup = BeautifulSoup(resp.text, 'html5lib')
    arts = soup.find_all('div', class_='r-ent')
    for art in arts:
        title = art.find('div', class_='title').getText().strip()
        if not title.startswith('(本文已被删除)'):
            link = 'https://www.ptt.cc' + \
                art.find('div', class_='title').a['href'].strip()
        author = art.find('div', class_='author').getText().strip()
        article = {
            'title': title,
            'link': link,
            'author': author
        }
        article_list.append(article)
        # print(f'title: {title}\nlink: {link}\nauthor: {author}')
    # 利用 Css Selector 定位下一页网址
    next_url = 'https://www.ptt.cc' + \
        soup.select_one(
            '#action-bar-container > div > div.btn-group.btn-group-paging > a:nth-child(2)')['href']
    return next_url

# 当执行此程序时成立
if __name__ == '__main__':
    # 第一个页面网址
    url = 'https://www.ptt.cc/bbs/Gossiping/index.html'
    # 先让爬虫爬 10 页
    for now_page_number in range(10):
        print(f'crawing {url}')
        resp = get_resp(url)
        if resp != 'error':
            url = get_articles(resp)
        print(f'======={now_page_number+1}/10=======')
    print(article_list)

接下来,要将存放所有文章资讯的串列存为 JSON 档案,我们使用的是 Python 中的 json 库(内建),记得将 json 引入。

import json

with open('ptt-articles.json', 'w', encoding='utf-8') as f:
    json.dump(article_list, f, indent=2,
              sort_keys=True, ensure_ascii=False)

再来将存为 JSON 档案的程序码加入爬虫专案当中。

import requests
import json
from bs4 import BeautifulSoup

article_list = []

def get_resp(url):
    cookies = {
        'over18': '1'
    }
    resp = requests.get(url, cookies=cookies)
    if resp.status_code != 200:
        return 'error'
    else:
        return resp

def get_articles(resp):
    soup = BeautifulSoup(resp.text, 'html5lib')
    arts = soup.find_all('div', class_='r-ent')
    for art in arts:
        title = art.find('div', class_='title').getText().strip()
        if not title.startswith('(本文已被删除)'):
            link = 'https://www.ptt.cc' + \
                art.find('div', class_='title').a['href'].strip()
        author = art.find('div', class_='author').getText().strip()
        article = {
            'title': title,
            'link': link,
            'author': author
        }
        article_list.append(article)
    # 利用 Css Selector 定位下一页网址
    next_url = 'https://www.ptt.cc' + \
        soup.select_one(
            '#action-bar-container > div > div.btn-group.btn-group-paging > a:nth-child(2)')['href']
    return next_url

# 当执行此程序时成立
if __name__ == '__main__':
    # 第一个页面网址
    url = 'https://www.ptt.cc/bbs/Gossiping/index.html'
    # 先让爬虫爬 10 页
    for now_page_number in range(10):
        print(f'crawing {url}')
        resp = get_resp(url)
        if resp != 'error':
            url = get_articles(resp)
        print(f'======={now_page_number+1}/10=======')
    # 将存放所有文章资讯的串列存於 JSON 档案中
    with open('ptt-articles.json', 'w', encoding='utf-8') as f:
        json.dump(article_list, f, indent=2,
                  sort_keys=True, ensure_ascii=False)

成功爬取连续多页面 ptt 文章资讯并存於 JSON 档中。

结语

今天将 PPT 爬虫加入了存为 JSON 档的功能,PTT 爬虫开发到目前为止。

明日内容

明天我们将实战开发爬取 ISO 映像的下载网址,会以 Ubuntu ISO 当开发例子。

补充资料

PTT 八卦版 : https://www.ptt.cc/bbs/Gossiping/index.html


<<:  [Day13] Slide In on Scroll

>>:  day13: 模组化好的写法 -单一功能原则(1)

EP16 - [TDD] 建立 Order 参数 (2/2)

Youtube 频道:https://www.youtube.com/c/kaochenlong ...

Microsoft Windows VirtualDesktop 系列纪录 - MSIX AppAttach

MSIX AppAttach 其实让我想到了十年前刚接触Citrix XenApp跟VMware V...

[Day 29] 使用ChromeDriver来做单元测试(二)

接下来我们新增一个测试档案 php artisan dusk:make UserDriverTest...

[从0到1] C#小乳牛 练成基础程序逻辑 Day 12 - 四大套路 读懂程序码 Sequence

套路程序码的4种方法 | 一步一步来 | DEMO 🐄点此填写今日份随堂测验 ...

Day19 Project2 - 留言板

今天就用昨天讲到的CRUD搭配RESTful API做出一个留言板,首先先来定义一下留言板会需要有的...