爬虫练习-获取imooc课程目录

代码:

kotlin 复制代码
from bs4 import BeautifulSoup
import requests
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0',
}

id='371'  #课程id
html=requests.get('https://coding.imooc.com/class/chapter/'+id+'.html#Anchor',headers=headers).text
print(html)
soup=BeautifulSoup(html,'lxml')
name=soup.find_all("span", "title_info")
for i in name:
    print(i.text)

效果:

代码分析:

kotlin 复制代码
from bs4 import BeautifulSoup
import requests
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0',
}

引入库

设置请求头

kotlin 复制代码
id='371'  #课程id
html=requests.get('https://coding.imooc.com/class/chapter/'+id+'.html#Anchor',headers=headers).text
print(html)
soup=BeautifulSoup(html,'lxml')

获取网页 使用BeautifulSoup解析

kotlin 复制代码
name=soup.find_all("span", "title_info")
for i in name:
    print(i.text)

获取全部 class为title_info的span标签

循环输出标签的text

相关推荐
Warson_L5 小时前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅5 小时前
海天线算法的前世今生
python·计算机视觉
韩师傅5 小时前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L5 小时前
LangGraph的MessageState and HumanMessage
python
韩师傅5 小时前
当你的甲方吐槽天空不够蓝,你应该如何应对
python·计算机视觉
Warson_L6 小时前
python的类&继承
python
Warson_L6 小时前
类型标注/type annotation
python
ThreeS8 小时前
手搓MiniVLA全实战教程-一步一步用pytorch解释原理与思路
人工智能·python
金銀銅鐵10 小时前
[Python] 模 n 乘法的逆元计算器
python·数学·游戏