无码av一区二区三区无码,在线观看老湿视频福利,日韩经典三级片,成 人色 网 站 欧美大片在线观看

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

爬蟲python案例

2023-02-22 18:30 作者:四季養(yǎng)花  | 我要投稿

import requests

from bs4 import BeautifulSoup

import csv


# 定義函數(shù)來獲取名人名言

def get_quotes(url):

? ? # 發(fā)送請(qǐng)求并獲取網(wǎng)頁內(nèi)容

? ? response = requests.get(url)

? ? html = response.text


? ? # 使用 BeautifulSoup 解析網(wǎng)頁內(nèi)容

? ? 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


# 定義函數(shù)來保存名人名言到 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']])


# 調(diào)用函數(shù)獲取名人名言并保存到 CSV 文件中

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

quotes = get_quotes(url)

save_quotes(quotes, 'quotes.csv')


爬蟲python案例的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國家法律
东丽区| 嘉兴市| 永登县| 固始县| 渑池县| 观塘区| 怀柔区| 昌图县| 股票| 安多县| 云安县| 敖汉旗| 屯昌县| 玉屏| 建瓯市| 黔江区| 武定县| 西城区| 扬中市| 瑞昌市| 衡阳市| 韶关市| 三亚市| 绍兴县| 衢州市| 周宁县| 福清市| 繁昌县| 赣州市| 苗栗市| 斗六市| 宁强县| 靖边县| 工布江达县| 宁乡县| 崇左市| 宁都县| 雷山县| 泗水县| 岢岚县| 保山市|