【问题标题】:bs4 scraping: download images and save them in local folder with desired namebs4 抓取:下载图像并将它们保存在本地文件夹中并使用所需的名称
【发布时间】:2018-09-16 05:36:59
【问题描述】:

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

【问题讨论】:

    标签: python image download beautifulsoup screen-scraping


    【解决方案1】:

    使用 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)) 
    

    【讨论】:

    • 感谢您的回答
    • 很高兴这可以帮助你:)
    猜你喜欢
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多