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

python 爬虫之 爬取网站信息并保存到文件

在这里插入图片描述

文章目录

  • 前期准备
  • 探索该网页的HTML码的特点
  • 开始编写代码
  • 存入文件
  • 总的程序
  • 文件存储效果

前期准备

随便找个网站进行爬取,这里我选择的是(一个卖书的网站)
https://www.bookschina.com/24hour/62700000/
在这里插入图片描述

我的目的是爬取这个网站的这个页面的书籍的名称以及相对应的价格

探索该网页的HTML码的特点

在该网页右键,选择检查,就可以看到下面的样子
在这里插入图片描述
然后按下面图片的第一个按键(作用是:当你鼠标停留在网页时,会自动显示到对应的网页代码)
在这里插入图片描述

查找书名的特点

在这里插入图片描述
在这里插入图片描述

我们发现,书名是位于<h2 class = "name" >标签的 <a >标签里面的

在这里插入图片描述

同理,可以找到价格是位于<div class = "priceWrap" 里面的<span class = "swllPrice>标签里面的 "

那么这么就好办了

开始编写代码

import requests
from bs4 import BeautifulSoup

# 设置请求头,模拟浏览器访问
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
}

# 发送GET请求获取页面内容
response = requests.get(r'https://www.bookschina.com/24hour/62700000/', headers=headers)

# 打印HTTP响应状态码
print(response.status_code)

# 获取页面内容
content = response.text

# 使用BeautifulSoup解析HTML
soup = BeautifulSoup(content, "html.parser")

# 存储书名的列表
namestore = []

# 存储价格的列表
pricestore = []

# 查找所有class为"name"的h2标签
allname = soup.findAll("h2", attrs={"class": "name"})

# 遍历每个h2标签
for name in allname:
    # 在每个h2标签中查找所有的a标签
    realnames = name.findAll("a")
    # 遍历每个a标签
    for realname in realnames:
        # 将书名添加到namestore列表中
        namestore.append(realname.string)

# 查找所有class为"priceWrap"的div标签
allprice = soup.findAll("div", attrs={"class": "priceWrap"})

# 遍历每个div标签
for price in allprice:
    # 在每个div标签中查找所有class为"sellPrice"的span标签
    realprices = price.findAll("span", attrs={"class": "sellPrice"})
    # 遍历每个span标签
    for realprice in realprices:
        # 将价格添加到pricestore列表中
        pricestore.append(realprice.string)

# 使用zip函数将书名和价格对应起来,并打印结果
for a, b in zip(namestore, pricestore):
    print(a, b)


存入文件



# 打开文件,准备写入数据,使用UTF-8编码
with open(r"d:\Desktop\畅销书以及价格.txt", "w", encoding='utf-8') as f:
    # 使用zip函数将书名和价格对应起来,并写入文件
    for a, b in zip(namestore, pricestore):
        # 写入书名
        f.write(str(a) + '\n')
        # 写入价格
        f.write(str(b) + '\n')

总的程序

import requests
from bs4 import BeautifulSoup


headers ={
    "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
}

responce = requests.get(r'https://www.bookschina.com/24hour/62700000/',headers = headers)

print(responce.status_code)
content = responce.text
soup = BeautifulSoup(content,"html.parser")

namestore = []
pricestore = []

allname = soup.findAll("h2",attrs={"class" : "name"})
for name in allname:
    realnames = name.findAll("a")
    for realname in realnames:
        #print(realname.string)
        namestore.append(realname.string)

allprice = soup.findAll("div",attrs={"class":"priceWrap"})
for price in allprice:
    realprices = price.findAll("span",attrs={"class" : "sellPrice"})
    for realprice in realprices:
        #print(realprice.string)
        pricestore.append(realprice.string)


with open(r"d:\Desktop\畅销书以及价格.txt","w",encoding='utf-8') as f:
    for a, b in zip(namestore, pricestore):
        f.writelines(str(a) + '\n' )
        f.writelines(str(b) + '\n' )



文件存储效果

在这里插入图片描述


http://www.kler.cn/news/148360.html

相关文章:

  • C++初阶--String类的使用
  • TCP 传输可靠性问题
  • DMX512协议及对接口电路的分析
  • openssl版本号解析
  • HTML新手入门笔记整理:HTML基本标签
  • 【DevOps】SonarQube 指标解读
  • Vue3 实现elementPlus的table列宽调整和拖拽
  • 最新版灵沐V3.3微信资源类小程序源码支持流量主
  • C语言面试之旅:掌握基础,探索深度(面试实战之c语言关键词中篇)
  • 在Spring Boot中使用ECharts绘制数据图表
  • 智慧化工~工厂设备检修和保全信息化智能化机制流程
  • 火狐挂代理访问问题Software is preventing Firefox from safely connecting to this site
  • Blazor Table 实现获取当前选中行的功能
  • pdf加密文件解密(pdf文件解密小工具)
  • Kotlin学习——kt里面的函数,高阶函数 函数式编程 扩展函数和属性
  • 神器!使用 patchworklib 库进行多图排版真棒啊
  • Dubbo相关的问题
  • 前端开发学习 (三) 列表功能
  • 【开源】基于JAVA的森林火灾预警系统
  • C++进阶篇5---番外-位图和布隆过滤器
  • 带着GPT-4V(ision)上路,自动驾驶新探索
  • 计算虚拟化之CPU——qemu解析
  • 距离向量路由协议——IGRP和EIGRP
  • Modown主题v8.12 安装教程和主题下载
  • 促进高层次人才创新创业,长沙又在“放大招”
  • uniapp使用map标签
  • 淼一科技为互联网企业销毁硬盘数据 拆除机房设备
  • Hive进阶函数:inline() 和 struct() ,一列转多行
  • git stash
  • 方舟笔记:方舟开发框架概述