爬取菜鸟教程的python100例
时间: 2023-09-28 12:04:34 浏览: 224
好的,我可以帮你实现这个功能。需要用到Python中的两个模块:requests和BeautifulSoup。
首先,使用requests发送HTTP请求获取菜鸟教程的Python100例页面的HTML代码:
```python
import requests
url = 'https://www.runoob.com/python/python-100-examples.html'
response = requests.get(url)
html = response.content.decode('utf-8')
```
接着,使用BeautifulSoup解析HTML代码,提取出Python100例的题目和代码:
```python
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
examples = soup.find_all('div', class_='example')
for example in examples:
title = example.h4.string
code = example.pre.code.string
print('题目:', title)
print('代码:\n', code)
```
这样,就可以爬取到菜鸟教程的Python100例的题目和代码了。完整代码如下:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.runoob.com/python/python-100-examples.html'
response = requests.get(url)
html = response.content.decode('utf-8')
soup = BeautifulSoup(html, 'html.parser')
examples = soup.find_all('div', class_='example')
for example in examples:
title = example.h4.string
code = example.pre.code.string
print('题目:', title)
print('代码:\n', code)
```
希望这个回答能够帮到你!
阅读全文