零代码编程:用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表内容:

相关推荐
码上生存指南1 小时前
我让 Claude、ChatGPT、Kimi 同时帮我写代码,差距有点大
ai·chatgpt
积跬步,慕至千里3 小时前
2026年2月读书笔记|AI大模型助你轻松搞定数据分析
语言模型·chatgpt
Agent产品评测局5 小时前
企业自动化项目,如何做好内部推广与员工培训?——企业级智能体落地与人才赋能实测指南
运维·人工智能·ai·chatgpt·自动化
向量引擎5 小时前
肝了三天三夜!四大AI模型(DeepSeek/Gemini/ChatGPT/豆包)深度横评,开发者该如何选?
人工智能·chatgpt·架构·开源·aigc·文心一言·api调用
AI-Ming7 小时前
程序员转行学习 AI 大模型: 模型微调| 附清晰概念分类
人工智能·pytorch·深度学习·机器学习·chatgpt·nlp·gpt-3
薛定猫AI1 天前
【深度解析】Claude Auto Dream:从“短期对话”到“项目级心智模型”的记忆系统升级
人工智能·chatgpt
树谷-胡老师1 天前
基于AI工具(ChatGPT、OpenClaw等)工作流的高强度论文写作实战
人工智能·chatgpt
承渊政道1 天前
从n-grams到Transformer:一文读懂语言模型基础
深度学习·学习·语言模型·自然语言处理·chatgpt·transformer·机器翻译
MeowNeko1 天前
为什么说程序员重命名时电脑不要带中文?记一次python manage.py runserver时UnicodeDecodeError的原因与解决方案
人工智能·python·chatgpt·中间件·django·utf8
智算菩萨2 天前
【实战讲解】ChatGPT 5.4深度文献检索完全指南:提示词工程与学术实战策略
论文阅读·人工智能·gpt·搜索引擎·chatgpt·提示词·论文笔记