python获取数据
时间: 2023-11-20 15:59:37 浏览: 37
Python获取数据的方法有很多种,以下是其中的几种常见方法:
1. 使用urllib库进行网页数据的获取
```python
import urllib.request
response = urllib.request.urlopen('http://www.example.com/')
html = response.read()
print(html)
```
2. 使用requests库进行网页数据的获取
```python
import requests
response = requests.get('http://www.example.com/')
html = response.text
print(html)
```
3. 使用BeautifulSoup库进行网页数据的解析
```python
import requests
from bs4 import BeautifulSoup
response = requests.get('http://www.example.com/')
soup = BeautifulSoup(response.text, 'html.parser')
print(soup.title)
```
4. 使用正则表达式进行数据的匹配
```python
import re
text = 'hello world'
result = re.findall('hello (\w+)', text)
print(result)
```
5. 从Excel表格中读取数据
```python
import xlrd
workbook = xlrd.open_workbook('example.xls')
sheet = workbook.sheet_by_index(0)
data = []
for i in range(sheet.nrows):
row = sheet.row_values(i)
data.append(row)
print(data)
```