前置知识: Python

Python与Web爬虫

00:00
1 min Intermediate 2026/6/14

Scrapy与BeautifulSoup

1. BeautifulSoup

from bs4 import BeautifulSoup
import requests

response = requests.get('https://example.com')
soup = BeautifulSoup(response.text, 'html.parser')
titles = [h2.text for h2 in soup.find_all('h2')]

2. Scrapy

class QuotesSpider(scrapy.Spider):
  name = 'quotes'
  start_urls = ['https://quotes.toscrape.com']

  def parse(self, response):
    for quote in response.css('div.quote'):
      yield {'text': quote.css('span::text').get()}

知识检测

学习进度

-- 已学文档
--% 知识覆盖率

学习推荐

专注模式