首页 > 解决方案 > bs4 抓取:下载图像并将它们保存在本地文件夹中并使用所需的名称

问题描述

我有打印我要下载的所有 image_url 的代码
接下来我想将它们保存在本地文件夹中,其中 folder_name=scrap_images
和所需的 image_name=uni_name 也在输出
中,你能帮我吗?

response   = requests.get('https://www.eduvision.edu.pk/admissions.php? 
discipline_type=Social-Sciences&sub_level=7&city=&pageNo=2',headers=header)
soup       = BeautifulSoup(response.content, 'html.parser')
data=soup.findAll('div',attrs={'class':'col-lg-12 col-xs-12'})[:-1]
for d in data:
   uni_name,comma,city = (d.findAll('a')[1].text).partition(',')
   print(uni_name)
   admis_img = d.img['src']
   if(d.img['src']=="images_post/nust.jpg"):
       admis_img= "https://www.eduvision.edu.pk/"+d.img['src']
       print(admis_img)
   else:
       print(admis_img)

标签: pythonimagedownloadbeautifulsoupscreen-scraping

解决方案


使用 python 下载图像的最简单方法是使用请求模块。

你可以在这里阅读更多关于它的信息,它将帮助你开始使用它,我也为你附​​上了一个代码示例。

您还可以参考此答案以更好地理解您的查询。

# importing the requests library 
import requests

`# api-endpoint 
URL = "http://maps.googleapis.com/maps/api/geocode/json"`

# location given here 
location = "ImageKit"

# defining a params dict for the parameters to be sent to the API 
PARAMS = {'address':location}

# sending get request and saving the response as response object 
r = requests.get(url = URL, params = PARAMS) 

# extracting data in json format 
data = r.json() 


# extracting latitude, longitude and formatted address  
# of the first matching location 
latitude = data['results'][0]['geometry']['location']['lat'] 
longitude = data['results'][0]['geometry']['location']['lng'] 
formatted_address = data['results'][0]['formatted_address'] 

# printing the output 
print("Latitude:%s\nLongitude:%s\nFormatted Address:%s"
      %(latitude, longitude,formatted_address)) 

推荐阅读