爬虫与反爬虫实现全流程

我选取的网页爬取的是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

相关推荐
群联云防护小杜2 分钟前
从一次 DDoS 的“死亡回放”看现代攻击链的进化
开发语言·python·linq
Ice__Cai6 分钟前
Flask 入门详解:从零开始构建 Web 应用
后端·python·flask·数据类型
霸敛7 分钟前
好家园房产中介网后台管理完整(python+flask+mysql)
开发语言·python·flask
HenryLin7 分钟前
SHAP值的核心概念
python
Darach10 分钟前
坐姿检测Python实现
人工智能·python
Ice__Cai11 分钟前
Flask 之 Request 对象详解:全面掌握请求数据处理
后端·python·flask·request·python web框架
hui函数11 分钟前
Flask高效数据库操作指南
数据库·python·flask
灵犀海棠12 分钟前
FLASK项目快速构建
后端·python·flask
灰阳阳20 分钟前
接口自动化测试大全(python+pytest+allure)
python·pytest·接口自动化·allure
Momentary_SixthSense26 分钟前
RESP协议
java·开发语言·javascript·redis·后端·python·mysql