爬取菜鸟教程python100例
时间: 2023-11-24 16:52:38 浏览: 114
100个Python小例子
5星 · 资源好评率100%
为了爬取菜鸟教程Python100例,我们可以使用Python中的requests和BeautifulSoup库。具体步骤如下:
1.使用requests库获取网页内容
```python
import requests
url = 'https://www.runoob.com/python/python-100-examples.html'
response = requests.get(url)
html = response.text
```
2.使用BeautifulSoup库解析网页内容
```python
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
examples = soup.find_all('div', class_='example')
```
3.提取每个例子的题目和代码
```python
for example in examples:
title = example.find('p', class_='title').text
code = example.find('div', class_='example-code').text
print(title)
print(code)
```
完整代码如下:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.runoob.com/python/python-100-examples.html'
response = requests.get(url)
html = response.text
soup = BeautifulSoup(html, 'html.parser')
examples = soup.find_all('div', class_='example')
for example in examples:
title = example.find('p', class_='title').text
code = example.find('div', class_='example-code').text
print(title)
print(code)
```
阅读全文