程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python learning notes (34) -- Acquisition of website files, pictures and tables

編輯:Python

Running error episode :RequestsDependencyWarning:requests Library version mismatch

RequestsDependencyWarning: urllib3 (1.26.8) or chardet (3.0.4) doesn't match a supported version!

Solution :

1、 see requests Version of the library :pip list

2、 Uninstall old version :pip uninstall requests

3、 Download the latest version :pip instsall requests Search results · PyPI

4、 Rerun code : normal
 

 

Code practice 1、: Tabular data in file form —— Access to the world bank project list

import requests
url='https://search.worldbank.org/api/projects/all.csv'
res=requests.get(url)
file=open(' World Bank project list .csv','wb')# Create a csv file , Select binary mode ‘wb' Open file
file.write(res.content)# Write specific file contents
file.close()

  Running results : Download successful

  Code practice 2、: Tabular data in file form —— Images are downloaded

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'# Just load the picture address
res=requests.get(url)
file=open(' Song veyron .jpg','wb')# Create a jpg file , Select binary mode ‘wb' Open file
file.write(res.content)# Write specific file contents
file.close()

Running results :

  Code practice 3、: Tabular data in the form of web pages —— Sina Finance block trading table

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) Get a list , So add [0] Extract the first element
print(table)
table.to_excel(' Block trading table .xlsx')# What's returned here is excel Table name , So the suffix should be added in quotation marks , Otherwise an error 

Running results :

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved