python - 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)
解决方案
使用 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))
推荐阅读
- swift - swift:使用字符串
- javascript - Highcharts 渲染 svg 图标不显示
- python - SSL 证书失败
- sql - 使用 sql IN 时,带有名为 @param 的 @query 返回空结果集
- ios - 如何在 Swift 中的层次结构中的类之间进行通信
- java - java class.getAnnotation(Class class) 返回 null
- javascript - 获取 TypeError: Object(...) is not a function with when changed Routes in React
- python - 从 S3 流式传输 / 分块 csv 到 Python
- vba - 使用数组过滤和修改数据
- php - 以下代码中的 mime 是什么?