爬虫与反爬虫实现全流程

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

相关推荐
zzwq.几秒前
Pandas数据合并完全指南:merge、concat、join从入门到精通
python·数据分析
wuyoula1 分钟前
全新轻量级高性能跨平台 AI聊天+AI网关桌面
服务器·开发语言·c++·人工智能
m0_716765231 分钟前
数据结构--单链表的插入、删除、查找详解
c语言·开发语言·数据结构·c++·笔记·学习·visual studio
Shorasul2 分钟前
c++ 跨平台线程封装 c++如何封装pthread和std--thread
jvm·数据库·python
2401_832635583 分钟前
CSS如何利用Sass简化CSS书写_通过嵌套与简写优化编码效率
jvm·数据库·python
2402_854808376 分钟前
如何处理MongoDB跨分片事务报错_4.2+分布式事务的限制与两阶段提交延迟
jvm·数据库·python
vegetablec11 分钟前
如何进行SQL数学计算_运用ROUND与CEIL处理数值精度
jvm·数据库·python
疯狂打码的少年11 分钟前
【Day13 Java转Python】装饰器、生成器与lambda——Python的函数式“三件套”
java·开发语言·python
石榴树下的七彩鱼14 分钟前
Python OCR 文字识别 API 接入完整教程
开发语言·人工智能·后端·python·ocr·api·图片识别
会飞的胖达喵15 分钟前
基于qt开发的RedisDesk
开发语言·qt