国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(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)遵守國家法律
涟源市| 南充市| 西贡区| 河南省| 方正县| 祁东县| 汝城县| 临邑县| 临朐县| 南木林县| 民乐县| 民丰县| 武山县| 太谷县| 田东县| 莱阳市| 扶余县| 台湾省| 如东县| 开江县| 吉安市| 砀山县| 白山市| 高淳县| 吉木乃县| 白山市| 资源县| 探索| 海晏县| 门源| 盐津县| 彭泽县| 南投市| 湘潭市| 红河县| 永安市| 黄浦区| 永修县| 左贡县| 台江县| 浦县|