爬虫与反爬虫实现全流程

我选取的网页爬取的是ppt nba

需要的工具:pycharm,浏览器

爬虫需要观察它的网页信息,然后开始首先爬取它的html,可以看到有人气,标题,日期,咨询

可以看到用get方法

python 复制代码
import requests
url="https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=https%3A%2F%2Fwww.ptt.cc%2Fbbs%2FNBA%2Findex.html&pos_id=lJ5YB9Tu"
respone=requests.get(url)
print(respone.text)

接下来把它下载下来

python 复制代码
with open('output.html','w',encoding='utf-8')as f:
    f.write(respone.text)

反爬虫使用header方法,观察User-Agent: AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36

放入header

python 复制代码
import requests
url="https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=https%3A%2F%2Fwww.ptt.cc%2Fbbs%2FNBA%2Findex.html&pos_id=lJ5YB9Tu"
headers={'User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'}
respone=requests.get(headers=headers)
# print(respone.text)
with open('output.html','w',encoding='utf-8')as f:
    f.write(respone.text)

接下来正式开始爬取自己想要的资料

去终端机安一下beautifulsoup

python 复制代码
pip install beautifulsoup4

解析获得文章列表parser

复制代码
soup beautifulSoup(respone.text,"html.parser" )

接下来把我们需要的值一个一个取出来

标题: div a title.text

人气:div class=nrec span

日期:div date

相关推荐
2301_7634724619 分钟前
C++20概念(Concepts)入门指南
开发语言·c++·算法
肖永威26 分钟前
macOS环境安装/卸载python实践笔记
笔记·python·macos
TechWJ42 分钟前
PyPTO编程范式深度解读:让NPU开发像写Python一样简单
开发语言·python·cann·pypto
枷锁—sha1 小时前
【SRC】SQL注入WAF 绕过应对策略(二)
网络·数据库·python·sql·安全·网络安全
abluckyboy1 小时前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法
lly2024061 小时前
C++ 文件和流
开发语言
m0_706653231 小时前
分布式系统安全通信
开发语言·c++·算法
喵手1 小时前
Python爬虫实战:构建各地统计局数据发布板块的自动化索引爬虫(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集数据csv导出·采集各地统计局数据发布数据·统计局数据采集
寻寻觅觅☆2 小时前
东华OJ-基础题-104-A == B ?(C++)
开发语言·c++
lightqjx2 小时前
【C++】unordered系列的封装
开发语言·c++·stl·unordered系列