欢迎光临散文网 会员登陆 & 注册

爬虫python案例

2023-02-22 18:30 作者:四季养花  | 我要投稿

import requests

from bs4 import BeautifulSoup

import csv


# 定义函数来获取名人名言

def get_quotes(url):

    # 发送请求并获取网页内容

    response = requests.get(url)

    html = response.text


    # 使用 BeautifulSoup 解析网页内容

    soup = BeautifulSoup(html, 'html.parser')

    quotes = soup.select('.quote')


    # 提取名人名言并将它们保存到列表中

    results = []

    for quote in quotes:

        text = quote.select_one('.text').text

        author = quote.select_one('.author').text

        results.append({'text': text, 'author': author})

    

    return results


# 定义函数来保存名人名言到 CSV 文件中

def save_quotes(quotes, filename):

    with open(filename, 'w', newline='') as file:

        writer = csv.writer(file)

        writer.writerow(['text', 'author'])

        for quote in quotes:

            writer.writerow([quote['text'], quote['author']])


# 调用函数获取名人名言并保存到 CSV 文件中

url = 'https://quotes.toscrape.com/'

quotes = get_quotes(url)

save_quotes(quotes, 'quotes.csv')


爬虫python案例的评论 (共 条)

分享到微博请遵守国家法律