在列表中查找以 jpg 结尾的项目
Finding an item which ends with jpg in a list
我想在以 jpg
、png
或类似图像类型结尾的列表中查找项目。
假设这是我的清单:
['http://www.ragalahari.com/index.aspx', 'http://www.ragalahari.com/index.aspx', 'http://www.ragalahari.com/news.aspx', 'http://imgcdn.raagalahari.com/feb2015/photosessions/shraddha-das-in-long-black-frock/shraddha-das-in-long-black-frock1t.jpg', 'http://imgcdn.raagalahari.com/feb2015/photosessions/shraddha-das-in-long-black-frock/shraddha-das-in-long-black-frock2t.jpg', 'http://imgcdn.raagalahari.com/feb2015/photosessions/shraddha-das-in-long-black-frock/shraddha-das-in-long-black-frock3t.jpg', 'http://www.ragalahari.com/images/facebookicon.png', 'http://www.ragalahari.com/images/searchicon.png']
我想查找以 jpg
或 png
结尾的 URLS。
我的代码到这里:
href_links = []
for a in soup.find_all('a', href=True):
href = a.get('href')
href = urljoin(link1, href)
href_links.append(href)
您可以使用 list comprehension and str.endswith
:
images = [x for x in href_links if x.endswith(('jpg', 'png'))]
您可以使用内置字符串 endswith
方法。
your_list = ["http://www.ragalahari.com/index.aspx",
"http://www.ragalahari.com/index.aspx",
"http://www.ragalahari.com/news.aspx",
"http://imgcdn.raagalahari.com/feb2015/photosessions/shraddha-das-in-long-black-frock/shraddha-das-in-long-black-frock1t.jpg",
"http://imgcdn.raagalahari.com/feb2015/photosessions/shraddha-das-in-long-black-frock/shraddha-das-in-long-black-frock2t.jpg",
"http://imgcdn.raagalahari.com/feb2015/photosessions/shraddha-das-in-long-black-frock/shraddha-das-in-long-black-frock3t.jpg",
"http://www.ragalahari.com/images/facebookicon.png",
"http://www.ragalahari.com/images/searchicon.png"]
img_links = []
for a in your_list:
if a.endswith('png') or a.endswith('jpg'):
img_links.append(a)
print img_links
结果 =
['http://imgcdn.raagalahari.com/feb2015/photosessions/shraddha-das-in-long-black-frock/shraddha-das-in-long-black-frock1t.jpg', 'http://imgcdn.raagalahari.com/feb2015/photosessions/shraddha-das-in-long-black-frock/shraddha-das-in-long-black-frock2t.jpg', 'http://imgcdn.raagalahari.com/feb2015/photosessions/shraddha-das-in-long-black-frock/shraddha-das-in-long-black-frock3t.jpg', 'http://www.ragalahari.com/images/facebookicon.png', 'http://www.ragalahari.com/images/searchicon.png']
我想在以 jpg
、png
或类似图像类型结尾的列表中查找项目。
假设这是我的清单:
['http://www.ragalahari.com/index.aspx', 'http://www.ragalahari.com/index.aspx', 'http://www.ragalahari.com/news.aspx', 'http://imgcdn.raagalahari.com/feb2015/photosessions/shraddha-das-in-long-black-frock/shraddha-das-in-long-black-frock1t.jpg', 'http://imgcdn.raagalahari.com/feb2015/photosessions/shraddha-das-in-long-black-frock/shraddha-das-in-long-black-frock2t.jpg', 'http://imgcdn.raagalahari.com/feb2015/photosessions/shraddha-das-in-long-black-frock/shraddha-das-in-long-black-frock3t.jpg', 'http://www.ragalahari.com/images/facebookicon.png', 'http://www.ragalahari.com/images/searchicon.png']
我想查找以 jpg
或 png
结尾的 URLS。
我的代码到这里:
href_links = []
for a in soup.find_all('a', href=True):
href = a.get('href')
href = urljoin(link1, href)
href_links.append(href)
您可以使用 list comprehension and str.endswith
:
images = [x for x in href_links if x.endswith(('jpg', 'png'))]
您可以使用内置字符串 endswith
方法。
your_list = ["http://www.ragalahari.com/index.aspx",
"http://www.ragalahari.com/index.aspx",
"http://www.ragalahari.com/news.aspx",
"http://imgcdn.raagalahari.com/feb2015/photosessions/shraddha-das-in-long-black-frock/shraddha-das-in-long-black-frock1t.jpg",
"http://imgcdn.raagalahari.com/feb2015/photosessions/shraddha-das-in-long-black-frock/shraddha-das-in-long-black-frock2t.jpg",
"http://imgcdn.raagalahari.com/feb2015/photosessions/shraddha-das-in-long-black-frock/shraddha-das-in-long-black-frock3t.jpg",
"http://www.ragalahari.com/images/facebookicon.png",
"http://www.ragalahari.com/images/searchicon.png"]
img_links = []
for a in your_list:
if a.endswith('png') or a.endswith('jpg'):
img_links.append(a)
print img_links
结果 =
['http://imgcdn.raagalahari.com/feb2015/photosessions/shraddha-das-in-long-black-frock/shraddha-das-in-long-black-frock1t.jpg', 'http://imgcdn.raagalahari.com/feb2015/photosessions/shraddha-das-in-long-black-frock/shraddha-das-in-long-black-frock2t.jpg', 'http://imgcdn.raagalahari.com/feb2015/photosessions/shraddha-das-in-long-black-frock/shraddha-das-in-long-black-frock3t.jpg', 'http://www.ragalahari.com/images/facebookicon.png', 'http://www.ragalahari.com/images/searchicon.png']