当前位置: 首页 > article >正文

Python脚本爬取目标网站上的所有链接

一、爬取后txt文件保存

需要先pip install requests和BeautifulSoup库

import requests
from bs4 import BeautifulSoup

# 定义要爬取的新闻网站URL
url = 'https://www.chinadaily.com.cn/'  # China Daily 网站

# 发送请求获取页面内容
response = requests.get(url)

# 检查请求是否成功
if response.status_code == 200:
    print('Successfully retrieved the website.')
    # 解析网页内容
    soup = BeautifulSoup(response.text, 'html.parser')

    # 打开一个文件以写入爬取的数据
    with open('news_data.txt', 'w', encoding='utf-8') as f:
        # 选择网站上合适的新闻标签
        for item in soup.find_all('a', href=True):  # 这里使用<a>标签,因为它包含链接
            title = item.get_text().strip()  # 获取标题
            link = item['href']  # 获取链接

            # 过滤掉无效的标题或链接
            if title and 'http' in link:
                # 将标题和链接写入文件
                f.write(f'链接标题: {title}\n链接地址: {link}\n\n')
        print("Data saved to 'news_data.txt'.")
else:
    print(f'Failed to retrieve the website. Status code: {response.status_code}')

二、 爬取后csv文件保存

import requests
from bs4 import BeautifulSoup
import csv

# 定义要爬取的新闻网站URL
url = 'https://www.chinadaily.com.cn/'  # 示例网站

# 发送请求获取页面内容
response = requests.get(url)

# 手动设置编码为utf-8(如果页面是使用utf-8编码)
response.encoding = 'utf-8'  # 确保使用正确的编码格式

# 检查请求是否成功
if response.status_code == 200:
    print('Successfully retrieved the website.')
    # 解析网页内容
    soup = BeautifulSoup(response.text, 'html.parser')

    # 打开一个CSV文件以写入爬取的数据
    with open('news_data.csv', 'w', newline='', encoding='utf-8') as f:
        writer = csv.writer(f)
        writer.writerow(['Title', 'Link'])  # 写入标题行

        # 查找所有包含链接的<a>标签
        for item in soup.find_all('a', href=True):
            title = item.get_text().strip()  # 获取标题
            link = item['href']  # 获取链接

            # 过滤掉无效的标题或链接
            if title and link:
                writer.writerow([title, link])
        print("Data saved to 'news_data.csv'.")
else:
    print(f'Failed to retrieve the website. Status code: {response.status_code}')


http://www.kler.cn/a/349131.html

相关文章:

  • 低代码工单管理app评测,功能与效率解析
  • LangChain使用Prompt02
  • Matlab实现野马优化算法优化回声状态网络模型 (WHO-ESN)(附源码)
  • 算法:238.除自身以外数组的乘积
  • 浙大数据结构:11-散列3 QQ帐户的申请与登陆
  • 植物大战僵尸杂交版
  • 运行Springboot + Vue 项目
  • 回归本真 治愈心灵——汪青《在西行的路上》
  • 地平线与英伟达工具链 PTQ 工具功能参数对比与实操
  • Linux下以编译源码的方式安装Qt5与Qt6及其使用
  • AB路线——BFS+分层图
  • Git---Git打标签
  • ui入门
  • Antsword-labs靶机渗透
  • Python基础语法条件
  • 基础IO -- 理解文件(1)
  • 使用tgz包下载安装clickhouse低版本
  • BERT--详解
  • 嵌入式Linux:信号掩码
  • 写一篇assignment的感悟