bs4抓取:下载图像并将其保存在具有所需名称的本地文件夹中

时间:2018-09-16 05:37:00

标签: python image download beautifulsoup screen-scraping

我有打印我要下载的所有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)

1 个答案:

答案 0 :(得分:0)

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

您可以阅读有关here的更多信息,它将帮助您开始使用它,我也为您提供了一个代码示例。

您也可以参考此answer,以更好地了解您的查询。

# 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))