bs4 抓取:下载图像并将它们保存在具有所需名称的本地文件夹中
bs4 scraping: download images and save them in local folder with desired name
我有打印我要下载的所有 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 下载图像的最简单方法是使用请求模块。
您可以阅读更多相关信息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))
我有打印我要下载的所有 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 下载图像的最简单方法是使用请求模块。
您可以阅读更多相关信息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))