爬虫与反爬虫实现全流程

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

相关推荐
你撅嘴真丑18 分钟前
第八章 - 贪心法
开发语言·c++·算法
2501_9071368220 分钟前
基于Python+QT6的移动硬盘弹出工具
python·软件需求
梵刹古音28 分钟前
【C语言】 浮点型(实型)变量
c语言·开发语言·嵌入式
历程里程碑28 分钟前
Linux 17 程序地址空间
linux·运维·服务器·开发语言·数据结构·笔记·排序算法
u01092727131 分钟前
模板元编程调试方法
开发语言·c++·算法
??(lxy)1 小时前
java高性能无锁队列——MpscLinkedQueue
java·开发语言
2501_907136821 小时前
python 界面元素控件库工具,可以看到python的可视控件和使用方法
python·软件需求
2301_765703141 小时前
开发一个简单的Python计算器
jvm·数据库·python
yj15581 小时前
客厅阳台改卧室需要注意什么?
python
2401_838472511 小时前
C++图形编程(OpenGL)
开发语言·c++·算法