爬虫与反爬虫实现全流程

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

相关推荐
离越词3 分钟前
C++day8作业
开发语言·c++·windows
小白学大数据12 分钟前
构建企业级Selenium爬虫:基于隧道代理的IP管理架构
爬虫·tcp/ip·selenium
℃CCCC18 分钟前
请求库-axios
开发语言·华为·网络请求·harmonyos·deveco studio·axios请求·arkts编程
ling__i24 分钟前
java day18
java·开发语言
矛取矛求24 分钟前
日期类的实现
开发语言·c++·算法
大翻哥哥35 分钟前
Python 2025:AI工程化与智能代理开发实战
开发语言·人工智能·python
站大爷IP1 小时前
Python文件处理:从基础操作到实战技巧全解析
python
在下雨5991 小时前
项目讲解1
开发语言·数据结构·c++·算法·单例模式
再努力"亿"点点1 小时前
Sklearn(机器学习)实战:鸢尾花数据集处理技巧
开发语言·python
费弗里1 小时前
无需云服务器!通过Plotly Cloud免费快捷部署Dash应用
python·dash