零代码编程:用ChatGPT绕过网站的Cloudflare防护爬取网页数据

打开这个网页,想爬取网页上的内容:

https://toppsta.com/books/series/29278/national-geographic-kids-readers-level-1

这个网页看起来很简单,但是一般手段根本无法获取源代码,因为网站使用了Cloudflare服务器进行防护。

可以使用Cloudscraper库来获取网页源代码,从而爬取网页数据,在ChatGPT中输入提示词:

你是一个Python编程专家,要完成一个网页爬取的任务,具体步骤如下:

打开网页:https://toppsta.com/books/series/29278/national-geographic-kids-readers-level-1

这个网页使用了Cloudflare服务器,要使用 Cloudscraper 绕过 Cloudflare的防护;

Cloudscraper 的使用示例:

import cloudscraper

scraper = cloudscraper.create_scraper()

url = "http://exampleofyourtargetwebsite.com"

info = scraper.get(url)

print(info.status_code)

soup = BeautifulSoup(info.text, "html.parser")

print(soup.find(class_ = "classgoeshere").get_text())

使用 Cloudscraper 获取网页源代码后,查找定位所有的h4标签,打印出所有的h4内容;

然后定位h4标签中的a标签,提取a标签的内容,打印出a标签的内容;

保存所有a标签内容到本地电脑E盘的Excel表格:name.xlsx;

打开本地电脑E盘的Excel表格:name.xlsx,删除其中所有的字符"National Geographic Kids Readers:"

注意:每一步都要输出信息

ChatGPT给出的源代码:

import cloudscraper

from bs4 import BeautifulSoup

import pandas as pd

print("创建 Cloudscraper 对象...")

scraper = cloudscraper.create_scraper()

url = "https://toppsta.com/books/series/29278/national-geographic-kids-readers-level-1"

print(f"获取网页:{url}...")

response = scraper.get(url)

print("状态码:", response.status_code)

soup = BeautifulSoup(response.text, "html.parser")

print("查找所有的h4标签...")

h4_tags = soup.find_all('h4')

h4_texts = tag.get_text().strip() for tag in h4_tags

print("所有的h4内容:")

print("\n".join(h4_texts))

print("查找h4标签中的a标签...")

a_texts = tag.find('a').get_text().strip() for tag in h4_tags if tag.find('a')

print("所有的a标签内容:")

print("\n".join(a_texts))

print("保存a标签内容到Excel表格...")

df = pd.DataFrame(a_texts, columns="Title")

df.to_excel('E:\\name.xlsx', index=False)

print("打开Excel表格,删除指定字符...")

df = pd.read_excel('E:\\name.xlsx')

df'Title' = df'Title'.str.replace('National Geographic Kids Readers:', '')

df.to_excel('E:\\name.xlsx', index=False)

print("完成!")

程序运行后,生成的Excel表内容:

相关推荐
lucas_AI19 小时前
点开一条 ChatGPT 分享链接,私聊就被端了:数学键盘 + YouTube JSONP 组成的 P1 漏洞链
chatgpt
奈落2420 小时前
全局切换器之后:从 ChatGPT 桌面端把聊天、工作、编程并进一个窗口,看“三合一”改了什么、没改什么
chatgpt·工作流·ai 编程
dozenyaoyida2 天前
AI与大模型新闻日报 | 2026-10-30
人工智能·ai·chatgpt·大模型·新闻
LaughingZhu2 天前
Product Hunt 热榜 2026-10-30:Empacer、Scape、Floot in ChatGPT
java·前端·数据库·人工智能·chatgpt·notion
卢卡上学2 天前
WorkBuddy内配置使用Claude、ChatGPT、Gemini、Grop模型
chatgpt·中转站·腾讯workbuddy
shennv8882 天前
ChatGPT Plus订阅后如何使用Codex?CLI安装、额度查询与项目开发实践
chatgpt
sg_knight2 天前
Bug 定位实战:把报错丢给 Codex,它是怎么修的
chatgpt·llm·bug·openai·agent·ai编程·codex
Maynor9963 天前
ChatGPT Pro 额度档位怎么理解:5x / 20x / 50x 使用笔记
ai·chatgpt·教程
玫瑰互动GEO3 天前
GEM优化广告六要素: 从命名一致到落地页双对齐的工程化落地
人工智能·ai·chatgpt·gem优化
shandianchengzi3 天前
【记录】LLM|ChatGPT连接Github并管理
chatgpt·github