运行出错小插曲:RequestsDependencyWarning:requests库版本不匹配
RequestsDependencyWarning: urllib3 (1.26.8) or chardet (3.0.4) doesn't match a supported version!
解决方案:
1、查看requests库的版本:pip list
2、卸载旧版本:pip uninstall requests
3、下载最新版本:pip instsall requests?Search results · PyPI
4、重新运行代码:正常 ? 
?
代码练习1、:文件形式的表格数据——世界银行项目表的获取
import requests
url='https://search.worldbank.org/api/projects/all.csv'
res=requests.get(url)
file=open('世界银行项目表.csv','wb')#创建一个csv文件,选择二进制模式‘wb'打开文件
file.write(res.content)#写入具体文件内容
file.close()
?运行结果:下载成功

?代码练习2、:文件形式的表格数据——图片下载
import requests
url='https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwx2.sinaimg.cn%2Flarge%2F006gM0gnly1fubsej8xztj32bc1jk1g7.jpg&refer=http%3A%2F%2Fwx2.sinaimg.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1647513020&t=7cbced00c08883ca013c2b1dd09c246f'#载入图片地址即可
res=requests.get(url)
file=open('宋威龙.jpg','wb')#创建一个jpg文件,选择二进制模式‘wb'打开文件
file.write(res.content)#写入具体文件内容
file.close()
运行结果:

?代码练习3、:网页形式的表格数据——新浪财经大宗交易表
import pandas as pd
url='http://vip.stock.finance.sina.com.cn/q/go.php/vInvestConsult/kind/dzjy/index.phtml'
table=pd.read_html(url)[0]#pd.read_html(url)获取的试一个列表,所以要加[0]提取第一个元素
print(table)
table.to_excel('大宗交易表.xlsx')#此处返回的是excel表名,所以后缀要加到引号内,不然报错
运行结果:

?
|