无码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)遵守國家法律
寻甸| 广灵县| 梅州市| 霍州市| 绥宁县| 阿合奇县| 伊金霍洛旗| 莱芜市| 睢宁县| 蒙城县| 大新县| 靖安县| 富阳市| 辽宁省| 屯门区| 庄浪县| 郧西县| 多伦县| 堆龙德庆县| 色达县| 长乐市| 阿鲁科尔沁旗| 顺义区| 蓬溪县| 五大连池市| 朔州市| 修文县| 苏州市| 克拉玛依市| 恩平市| 临西县| 广南县| 黄浦区| 安乡县| 疏勒县| 砀山县| 柞水县| 彩票| 申扎县| 青龙| 晋城|