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

python爬取网页数据为json该用什么方法?

Python 爬取网页数据并保存为 JSON 的方法

在 Python 中,爬取网页数据并将其保存为 JSON 格式是一个常见的任务。以下是一些常用的方法和具体代码实现,结合最新的搜索结果。

方法1:使用 requestsjson 模块

requests 是一个常用的 HTTP 库,用于发送网络请求。结合 json 模块,可以轻松地将爬取的数据保存为 JSON 格式。

Python复制

import requests
import json

# 爬取网页数据
url = "https://en.wikipedia.org/wiki/Web_scraping"
response = requests.get(url)
data = response.text

# 将数据保存为 JSON
with open("data.json", "w", encoding="utf-8") as file:
    json.dump(data, file, ensure_ascii=False, indent=4)
方法2:使用 BeautifulSoupjson 模块

BeautifulSoup 是一个用于解析 HTML 和 XML 文档的库,结合 json 模块可以提取特定的网页内容并保存为 JSON 格式。

Python复制

import requests
from bs4 import BeautifulSoup
import json

# 爬取网页数据
url = "https://en.wikipedia.org/wiki/Web_scraping"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")

# 提取特定内容
titles = []
for tag in soup.find_all(["h1", "h2", "h3", "h4", "h5"]):
    titles.append({"tag": tag.name, "text": tag.text.strip()})

# 将数据保存为 JSON
with open("titles.json", "w", encoding="utf-8") as file:
    json.dump(titles, file, ensure_ascii=False, indent=4)
方法3:使用 Scrapyjson 模块

Scrapy 是一个强大的爬虫框架,适用于大规模的爬取任务。以下是一个简单的 Scrapy 爬虫示例,用于爬取网页数据并保存为 JSON。

Python复制

import scrapy
import json

class Spider(scrapy.Spider):
    name = "bored"

    def start_requests(self):
        url = "https://www.boredapi.com/api/activity"
        yield scrapy.Request(url, self.parse)

    def parse(self, response):
        data = json.loads(response.text)
        activity = data["activity"]
        type = data["type"]
        participants = data["participants"]
        yield {"Activity": activity, "Type": type, "Participants": participants}

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

bash复制

scrapy runspider <file_name> -o output.json
方法4:使用第三方 API 服务(如 Scrapeless)

对于不想处理复杂爬虫逻辑的用户,可以使用第三方 API 服务,如 Scrapeless,来简化爬取过程。

Python复制

import http.client
import json

conn = http.client.HTTPSConnection("api.scrapeless.com")
payload = json.dumps({
    "actor": "scraper.amazon",
    "input": {
        "url": "https://www.amazon.com/dp/B0BQXHK363",
        "action": "product"
    }
})
headers = {
    'Content-Type': 'application/json'
}
conn.request("POST", "/api/v1/scraper/request", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
方法5:使用 Seleniumjson 模块

Selenium 是一个用于自动化浏览器操作的工具,适用于动态网页的爬取。

Python复制

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import json

# 设置 WebDriver
options = Options()
options.add_argument("--headless")
driver = webdriver.Chrome(service=Service(), options=options)

# 爬取网页数据
url = "https://en.wikipedia.org/wiki/Web_scraping"
driver.get(url)

# 提取特定内容
titles = []
for tag in driver.find_elements(By.CSS_SELECTOR, "h1, h2, h3, h4, h5"):
    titles.append({"tag": tag.tag_name, "text": tag.text.strip()})

# 将数据保存为 JSON
with open("titles.json", "w", encoding="utf-8") as file:
    json.dump(titles, file, ensure_ascii=False, indent=4)

driver.quit()

总结

爬取网页数据并保存为 JSON 格式可以通过多种方法实现,具体选择取决于你的需求和偏好。对于简单的任务,requestsBeautifulSoup 是不错的选择;对于更复杂的任务,Scrapy 提供了强大的功能;而第三方 API 服务如 Scrapeless 可以进一步简化爬取过程。无论哪种方法,都可以通过 json 模块轻松地将数据保存为 JSON 格式。


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

相关文章:

  • 鸿蒙Harmony-双向数据绑定MVVM以及$$语法糖介绍
  • 【怎么用系列】短视频戒除—1—对推荐算法进行干扰
  • 【实践案例】基于大语言模型的海龟汤游戏
  • Day51:type()函数
  • 如何解决云台重力补偿?
  • 系统URL整合系列视频一(需求方案)
  • 验证工具:GVIM和VIM
  • java s7接收Byte字节,接收word转16位二进制
  • 当大模型遇上Spark:解锁大数据处理新姿势
  • docker Error response from daemon: Get “https://registry-1.docker.io/v2/ 的问题处理
  • AI工具如何辅助写文章(科研版)
  • 无缝切换?从Vue到React
  • PostIn简明安装教程(入门级)
  • 企业四要素如何用Java进行调用
  • 【多线程】线程池核心数到底如何配置?
  • 如何设置Jsoup爬虫的User-Agent?
  • 拉取本地的 Docker 镜像的三种方法
  • 解决DeepSeek服务器繁忙问题:本地部署与优化方案
  • windows下玩转vllm:vllm简介
  • 【怎么用系列】短视频戒除-1-对推荐算法进行干扰
  • 怎么让PDF文档变小一点?
  • 求分数序列和(信息学奥赛一本通-1078)
  • 【算法篇】贪心算法
  • 【Elasticsearch】索引性能优化
  • 自指学习:AGI的元认知突破
  • python读取excel工具:openpyxl | AI应用开发