爬虫开发案例&项目源码

下面是一个使用Python开发的简单网页爬虫案例及相关项目源码。这个项目将展示如何使用requestsBeautifulSoup库来抓取网页内容,并提取特定的信息。

项目简介

该项目展示了如何编写一个简单的网页爬虫来抓取网页上的新闻标题和链接。我们将以一个新闻网站(例如BBC新闻)为例,抓取首页上的新闻标题和链接。

目录结构

复制代码
news_scraper/
├── scraper.py
├── requirements.txt
└── README.md

1. 准备工作

安装依赖

创建一个requirements.txt文件,内容如下:

txt
复制代码
requests
beautifulsoup4

使用以下命令安装依赖:

bash
复制代码
pip install -r requirements.txt

2. 爬虫代码

scraper.py
python
复制代码
import requests
from bs4 import BeautifulSoup

def fetch_news(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        return response.text
    else:
        print(f"Failed to retrieve content: {response.status_code}")
        return None

def parse_news(html_content):
    soup = BeautifulSoup(html_content, 'html.parser')
    news_list = []
    for item in soup.find_all('a', class_='gs-c-promo-heading'):
        title = item.get_text()
        link = item['href']
        if link.startswith('/'):
            link = f"https://www.bbc.com{link}"
        news_list.append({'title': title, 'link': link})
    return news_list

def save_news(news_list, filename='news.txt'):
    with open(filename, 'w') as file:
        for news in news_list:
            file.write(f"{news['title']}\n{news['link']}\n\n")

def main():
    url = "https://www.bbc.com/news"
    html_content = fetch_news(url)
    if html_content:
        news_list = parse_news(html_content)
        save_news(news_list)
        print(f"Saved {len(news_list)} news articles to news.txt")

if __name__ == "__main__":
    main()

3. 代码解析

  • fetch_news(url)

    • 使用requests库发送HTTP GET请求,获取网页内容。

    • 设置User-Agent头以模拟浏览器请求,防止被目标网站阻止。

    • 返回网页内容的HTML文本。

  • parse_news(html_content)

    • 使用BeautifulSoup解析HTML内容。

    • 查找所有新闻标题和链接(假设它们位于带有class_='gs-c-promo-heading'<a>标签中)。

    • 构建新闻标题和链接的列表。

  • save_news(news_list, filename='news.txt')

    • 将新闻标题和链接保存到文本文件中。

  • main()

    • 定义要抓取的新闻网站URL。

    • 获取并解析网页内容,提取新闻标题和链接。

    • 将提取到的新闻标题和链接保存到文件中。

4. 运行爬虫

在终端中运行以下命令:

bash
复制代码
python scraper.py

5. 项目说明

该项目演示了如何使用Python编写一个简单的网页爬虫,抓取BBC新闻首页上的新闻标题和链接,并将其保存到本地文件中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值