Python爬虫开发:BeautifulSoup、Scrapy入门

在现代网络开发中,网络爬虫是一个非常重要的工具。它可以自动化地从网页中提取数据,并且可以用于各种用途,如数据收集、信息聚合和内容监控等。在Python中,有多个库可以用于爬虫开发,其中BeautifulSoup和Scrapy是两个非常流行的选择。本篇文章将详细介绍这两个库,并提供一个综合详细的例子,展示如何使用它们来进行网页数据爬取。

一、BeautifulSoup入门

1. BeautifulSoup简介

BeautifulSoup是一个Python库,用于从HTML或XML文档中提取数据。它能够通过标签和属性来定位和提取数据,非常适合进行小规模的网页抓取任务。

2. 安装BeautifulSoup

在使用BeautifulSoup之前,需要安装它和一个HTML解析器,如lxml或html5lib。可以使用以下命令进行安装:

pip install beautifulsoup4 lxml
3. BeautifulSoup基础用法

以下是BeautifulSoup的基本用法,包括如何解析HTML文档,查找标签和属性,以及提取数据。

from bs4 import BeautifulSoup

html_doc = """
<html>
<head><title>示例页面</title></head>
<body>
<p class="title"><b>示例段落</b></p>
<p class="content">这是一个示例页面。</p>
<a href="http://example.com/one" class="link">第一个链接</a>
<a href="http://example.com/two" class="link">第二个链接</a>
</body>
</html>
"""

soup = BeautifulSoup(html_doc, 'lxml')

# 查找标题标签
title = soup.title
print(title.string)

# 查找所有段落标签
paragraphs = soup.find_all('p')
for p in paragraphs:
    print(p.text)

# 查找所有链接标签
links = soup.find_all('a')
for link in links:
    print(link.get('href'))

二、Scrapy入门

1. Scrapy简介

Scrapy是一个用于爬取网站并提取结构化数据的应用框架。它提供了强大的功能,如处理请求、解析HTML、管理爬取的数据等,适合进行大规模的爬虫开发。

2. 安装Scrapy

可以使用以下命令安装Scrapy:

pip install scrapy
3. Scrapy基础用法

以下是Scrapy的基本用法,包括如何创建项目、定义爬虫和解析数据。

# 创建Scrapy项目
scrapy startproject example_project
cd example_project

# 创建爬虫
scrapy genspider example example.com

example_project/spiders/example.py中定义爬虫:

import scrapy

class ExampleSpider(scrapy.Spider):
    name = "example"
    allowed_domains = ["example.com"]
    start_urls = [
        'http://example.com/',
    ]

    def parse(self, response):
        for title in response.css('title'):
            yield {'title': title.get()}

        for link in response.css('a::attr(href)').getall():
            yield response.follow(link, self.parse)

运行爬虫:

scrapy crawl example

三、综合示例:爬取博客文章

以下是一个综合示例,展示如何使用BeautifulSoup和Scrapy来爬取博客文章并提取文章标题和链接。

1. 使用BeautifulSoup爬取博客文章
import requests
from bs4 import BeautifulSoup

url = 'https://example-blog.com'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'lxml')

# 提取文章标题和链接
articles = soup.find_all('article')
for article in articles:
    title = article.find('h2').text
    link = article.find('a')['href']
    print(f"标题: {title}, 链接: {link}")
2. 使用Scrapy爬取博客文章

首先,创建Scrapy项目并生成爬虫:

scrapy startproject blog_crawler
cd blog_crawler
scrapy genspider blog_spider example-blog.com

blog_crawler/spiders/blog_spider.py中定义爬虫:

import scrapy

class BlogSpider(scrapy.Spider):
    name = "blog_spider"
    allowed_domains = ["example-blog.com"]
    start_urls = [
        'https://example-blog.com/',
    ]

    def parse(self, response):
        for article in response.css('article'):
            title = article.css('h2::text').get()
            link = article.css('a::attr(href)').get()
            yield {'title': title, 'link': link}

        next_page = response.css('a.next::attr(href)').get()
        if next_page:
            yield response.follow(next_page, self.parse)

运行爬虫并保存结果到JSON文件:

scrapy crawl blog_spider -o articles.json

四、深入理解BeautifulSoup

1. BeautifulSoup的解析器

BeautifulSoup支持多种解析器,包括Python标准库的html.parser、第三方库lxmlhtml5lib。不同解析器的性能和功能有所不同,选择适合的解析器可以提升解析效率。

from bs4 import BeautifulSoup

# 使用html.parser解析器
soup = BeautifulSoup(html_doc, 'html.parser')

# 使用lxml解析器
soup = BeautifulSoup(html_doc, 'lxml')

# 使用html5lib解析器
soup = BeautifulSoup(html_doc, 'html5lib')
2. BeautifulSoup的常用功能
  • 查找标签:使用findfind_all方法查找单个或多个标签。
  • CSS选择器:使用select方法通过CSS选择器查找标签。
  • 遍历文档树:使用parentchildrensiblings等方法遍历文档树。
# 查找单个标签
title_tag = soup.find('title')

# 查找所有特定标签
links = soup.find_all('a')

# 使用CSS选择器
links = soup.select('a')

# 遍历文档树
parent = title_tag.parent
siblings = title_tag.next_siblings
3. BeautifulSoup的应用实例

以下是一个完整的实例,展示如何使用BeautifulSoup爬取一个新闻网站的标题和链接。

import requests
from bs4 import BeautifulSoup

url = 'https://news.ycombinator.com/'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'lxml')

articles = soup.find_all('a', class_='storylink')
for article in articles:
    title = article.text
    link = article['href']
    print(f"标题: {title}, 链接: {link}")

五、深入理解Scrapy

1. Scrapy的组件

Scrapy有多个重要的组件,每个组件都有特定的功能。

  • Spider:定义爬取逻辑,发送请求并处理响应。
  • Item:定义数据结构,用于存储爬取的数据。
  • Pipeline:处理爬取的数据,如清洗、验证和存储。
  • Middleware:处理请求和响应,如添加请求头和处理错误。
2. Scrapy的配置

Scrapy提供了丰富的配置选项,可以在settings.py中配置。

# 设置用户代理
USER_AGENT = 'my-crawler (http://example.com)'

# 设置并发请求数量
CONCURRENT_REQUESTS = 16

# 设置下载延迟
DOWNLOAD_DELAY = 1

# 启用或禁用中间件
DOWNLOADER_MIDDLEWARES = {
    'myproject.middlewares.CustomMiddleware': 543,
}
3. Scrapy的应用实例

以下是一个完整的Scrapy爬虫实例,展示如何爬取一个新闻网站的标题和链接,并将数据存储到JSON文件中。

首先,创建项目和爬虫:

scrapy startproject news_crawler
cd news_crawler
scrapy genspider news_spider news.ycombinator.com

news_crawler/items.py中定义Item:

import scrapy

class NewsItem(scrapy.Item):
    title = scrapy.Field()
    link = scrapy.Field()

news_crawler/spiders/news_spider.py中定义爬虫:

import scrapy
from news_crawler.items import NewsItem

class NewsSpider(scrapy.Spider):
    name = 'news_spider'
    allowed_domains = ['news.ycombinator.com']
    start_urls = ['https://news.ycombinator.com/']

    def parse(self, response):
        articles = response.css('a.storylink')
        for article in articles:
            item = NewsItem()
            item['title'] = article.css('::text').get()
            item['link'] = article.css('::attr(href)').get()
            yield item

        next_page = response.css('a.morelink::attr(href)').get()
        if next_page:
            yield response.follow(next_page, self.parse)

news_crawler/pipelines.py中定义Pipeline:

import json

class NewsCrawlerPipeline:
    def open_spider(self, spider):
        self.file = open('items.json', 'w')

    def close_spider(self, spider):
        self.file.close()

    def process_item(self, item, spider):
        line = json.dumps(dict(item)) + "\n"
        self.file.write(line)
        return item

news_crawler/settings.py中启用Pipeline:

ITEM_PIPELINES = {
    'news_crawler.pipelines.NewsCrawlerPipeline': 300,
}

运行爬虫并保存结果到JSON文件:

scrapy crawl news_spider

六、总结

通过本文,我们详细介绍了Python中的两个流行的爬虫开发库:BeautifulSoup和Scrapy。我们不仅介绍了它们的基本用法,还深入探讨了它们的高级功能和应用场景。通过综合实例,我们展示了如何使用这两个库来爬取新闻网站的标题和链接,并将数据存储到文件中。

希望本文对你理解和使用BeautifulSoup和Scrapy有所帮助,无论是进行小规模的网页抓取任务,还是开发大规模的爬虫项目。未来可以根据具体需求选择合适的工具,提高开发效率和数据处理能力。

作者:Rjdeng
链接:https://juejin.cn/post/7400255677804232716

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值