-1

我有一个脚本可以从 bing 搜索中获取图像的 url。

URLA = "https://mykey:mykey@api.datamarket.azure.com/Bing/Search/Web?$format=json&Query=%(q)s"
API_KEY = 'mykey'

def requester(que, **params):
    print que
    r = requests.get(URLA % {'q': que}, auth=('', API_KEY))
    return r.json()['d']['results']

o = requester("%27HarryPotter%27")
print o

r.json() ['d']['results']返回这个:

[{u'Thumbnail': {u'ContentType': u'image/jpg', u'MediaUrl': u'http://ts3.mm.bing.net/th?id=H.4893399089021934&pid=15.1', u'__metadata': {u'type': u'Bing.Thumbnail'}, u'Height': u'225', u'Width': u'300', u'FileSize': u'12169'}, u'SourceUrl': u'http://omalapronta.wordpress.com/2011/06/06/harry-potter-tambem-e-viagem/', u'ContentType': u'image/jpeg', u'MediaUrl': u'http://omalapronta.files.wordpress.com/2011/06/harry-potter.jpg', u'__metadata': {u'type': u'ImageResult', u'uri': u"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query='HarryPotter'&$skip=0&$top=1"}, u'DisplayUrl': u'omalapronta.wordpress.com/2011/06/06/harry-potter-tambem-e-viagem', u'Width': u'1280', u'FileSize': u'262952', u'Title': u'Harry Potter tamb\xe9m \xe9 viagem! | O Mala Pronta', u'Height': u'960', u'ID': u'bea3b9dc-1c5c-4636-87e1-fb8e89d2705e'}, {u'Thumbnail': {u'ContentType': u'image/jpg', u'MediaUrl': u'http://ts3.mm.bing.net/th?id=H.4504674504213882&pid=15.1', u'__metadata': {u'type': u'Bing.Thumbnail'}, u'Height': u'225', u'Width': u'300', u'FileSize': u'14050'}, u'SourceUrl': u'http://www.whitegadget.com/pc-wallpapers/140110-harry-potter.html', u'ContentType': u'image/jpeg', u'MediaUrl': u'http://www.whitegadget.com/attachments/pc-wallpapers/65749d1313045554-harry-potter-harry-potter-deathly-hallows-part-2-2011-hollywood-movie-watch-online.jpg', u'__metadata': {u'type': u'ImageResult', u'uri': u"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query='HarryPotter'&$skip=1&$top=1"}, u'DisplayUrl': u'www.whitegadget.com/pc-wallpapers/140110-harry-potter.html', u'Width': u'1600', u'FileSize': u'369655', u'Title': u'65749d1313045554-harry-potter-harry-potter-deathly-hallows-part-2-2011 ...', u'Height': u'1200', u'ID': u'230b7b74-856a-4768-b6f7-064ea8e89503'}, {u'Thumbnail': {u'ContentType': u'image/jpg', u'MediaUrl': u'http://ts3.mm.bing.net/th?id=H.5049340749350366&pid=15.1', u'__metadata': {u'type': u'Bing.Thumbnail'}, u'Height': u'225', u'Width': u'300', u'FileSize': u'19526'}, u'SourceUrl': u'http://about-google-android.blogspot.com/2012/09/5-best-harry-potter-android-games-for.html', u'ContentType': u'image/jpeg', u'MediaUrl': u'http://1.bp.blogspot.com/-1nCYVVrJT8Y/UFMUTJyIyCI/AAAAAAAABT8/CfuTFgNHcJ4/s1600/Harry-Potter-harry-potter-and-the-order-of-the-phoenix-24888433-1024-768.jpg', u'__metadata': {u'type': u'ImageResult', u'uri': u"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query='HarryPotter'&$skip=2&$top=1"}, u'DisplayUrl': u'about-google-android.blogspot.com/2012/09/5-best-harry-potter...', u'Width': u'1024', u'FileSize': u'369989', u'Title': u'Best Harry Potter Android Games for Harry Potter Fans - News and ...', u'Height': u'768', u'ID': u'0e37f0f8-dc35-4330-92ec-9d4203c82b6f'}, {u'Thumbnail': {u'ContentType': u'image/jpg', u'MediaUrl': u'http://ts1.mm.bing.net/th?id=H.4806967151166936&pid=15.1', u'__metadata': {u'type': u'Bing.Thumbnail'}, u'Height': u'225', u'Width': u'300', u'FileSize': u'14762'}, u'SourceUrl': u'http://www.trucospc.info/fondos-de-pantalla/Harry-potter/index.asp', u'ContentType': u'image/jpeg', u'MediaUrl': u'http://www.trucospc.info/fondos-de-pantalla/Harry-potter/imagenes/Harry-potter-02.jpg', u'__metadata': {u'type': u'ImageResult', u'uri': u"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query='HarryPotter'&$skip=3&$top=1"}, u'DisplayUrl': u'www.trucospc.info/fondos-de-pantalla/Harry-potter/index.asp', u'Width': u'1024', u'FileSize': u'254844', u'Title': u'... PC > Fondos de pantalla de Harry potter - Wallpapers de Harry potter', u'Height': u'768', u'ID': u'be1db74f-cf18-494e-9d55-a63ca62f3812'}, {u'Thumbnail': {u'ContentType': u'image/jpg', u'MediaUrl': u'http://ts2.mm.bing.net/th?id=H.4831233706886625&pid=15.1', u'__metadata': {u'type': u'Bing.Thumbnail'}, u'Height': u'240', u'Width': u'300', u'FileSize': u'13409'}, u'SourceUrl': u'http://www.fanpop.com/clubs/harry-potter/images/24869135/title', u'ContentType': u'image/jpeg', u'MediaUrl': u'http://images5.fanpop.com/image/photos/24800000/Harry-Potter-harry-potter-24869135-1280-1024.jpg', u'__metadata': {u'type': u'ImageResult', u'uri': u"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query='HarryPotter'&$skip=4&$top=1"}, u'DisplayUrl': u'www.fanpop.com/clubs/harry-potter/images/24869135/title', u'Width': u'1280', u'FileSize': u'296417', u'Title': u'Harry Potter - Harry Potter Photo (24869135) - Fanpop fanclubs', u'Height': u'1024', u'ID': u'c522be2f-9b14-4653-9931-4ab32f25b6bb'}, {u'Thumbnail': {u'ContentType': u'image/jpg', u'MediaUrl': u'http://ts4.mm.bing.net/th?id=H.4561956467900979&pid=15.1', u'__metadata': {u'type': u'Bing.Thumbnail'}, u'Height': u'187', u'Width': u'300', u'FileSize': u'10492'}, u'SourceUrl': u'http://www.fanpop.com/clubs/harry-potter/images/33045859/title/harry-potter-wallpaper', u'ContentType': u'image/jpeg', u'MediaUrl': u'http://images6.fanpop.com/image/photos/33000000/Harry-Potter-harry-potter-33045859-1920-1200.jpg', u'__metadata': {u'type': u'ImageResult', u'uri': u"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query='HarryPotter'&$skip=5&$top=1"}, u'DisplayUrl': u'www.fanpop.com/clubs/harry-potter/images/33045859/title/harry...', u'Width': u'1920', u'FileSize': u'581553', u'Title': u'Harry Potter - Harry Potter Wallpaper (33045859) - Fanpop fanclubs', u'Height': u'1200', u'ID': u'2f3524af-1c8f-43f6-bd88-9d3758c2f4da'}, {u'Thumbnail': {u'ContentType': u'image/jpg', u'MediaUrl': u'http://ts4.mm.bing.net/th?id=H.4643698236720811&pid=15.1', u'__metadata': {u'type': u'Bing.Thumbnail'}, u'Height': u'225', u'Width': u'300', u'FileSize': u'11822'}, u'SourceUrl': u'http://coachingymetaforas.blogspot.com/2011/10/harry-potter-y-la-magia-de-los-textos.html', u'ContentType': u'image/jpeg', u'MediaUrl': u'http://1.bp.blogspot.com/-D79uXqQMxTk/TqHvdRgT0sI/AAAAAAAAAJw/Vs4cZG_lRPU/s1600/Harry_Potter_27.jpg', u'__metadata': {u'type': u'ImageResult', u'uri': u"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query='HarryPotter'&$skip=6&$top=1"}, u'DisplayUrl': u'coachingymetaforas.blogspot.com/2011/10/harry-potter-y-la-magia-de...', u'Width': u'1024', u'FileSize': u'215170', u'Title': u'Coaching y Met\xe1foras: Harry Potter y la Magia de los Textos', u'Height': u'768', u'ID': u'55966ed1-480b-46d5-afda-65b3aa0a2e07'}, {u'Thumbnail': {u'ContentType': u'image/jpg', u'MediaUrl': u'http://ts2.mm.bing.net/th?id=H.5003212789908665&pid=15.1', u'__metadata': {u'type': u'Bing.Thumbnail'}, u'Height': u'225', u'Width': u'300', u'FileSize': u'14315'}, u'SourceUrl': u'http://www.fanpop.com/clubs/harry-potter/images/32990483/title/hp-characters-wallpaper', u'ContentType': u'image/jpeg', u'MediaUrl': u'http://images6.fanpop.com/image/photos/32900000/HP-characters-harry-potter-32990483-1280-960.jpg', u'__metadata': {u'type': u'ImageResult', u'uri': u"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query='HarryPotter'&$skip=7&$top=1"}, u'DisplayUrl': u'www.fanpop.com/clubs/harry-potter/images/32990483/title/hp...', u'Width': u'1280', u'FileSize': u'329721', u'Title': u'HP characters - Harry Potter Wallpaper (32990483) - Fanpop fanclubs', u'Height': u'960', u'ID': u'7a4a48f2-e76a-478c-b32e-65cb6c2c0f8a'}, {u'Thumbnail': {u'ContentType': u'image/jpg', u'MediaUrl': u'http://ts3.mm.bing.net/th?id=H.4893137064362102&pid=15.1', u'__metadata': {u'type': u'Bing.Thumbnail'}, u'Height': u'240', u'Width': u'300', u'FileSize': u'18231'}, u'SourceUrl': u'http://www.fanpop.com/clubs/harry-potter/images/16692602/title/harry-harry-potter-deathly-hallows-wallpaper', u'ContentType': u'image/jpeg', u'MediaUrl': u'http://images4.fanpop.com/image/photos/16600000/Harry-Harry-Potter-And-The-Deathly-Hallows-harry-potter-16692602-1280-1024.jpg', u'__metadata': {u'type': u'ImageResult', u'uri': u"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query='HarryPotter'&$skip=8&$top=1"}, u'DisplayUrl': u'www.fanpop.com/clubs/harry-potter/images/16692602/title/harry-harry...', u'Width': u'1280', u'FileSize': u'1397584', u'Title': u'Harry - Harry Potter And The Deathly Hallows - Harry Potter Wallpaper ...', u'Height': u'1024', u'ID': u'ec4d67b3-ec8a-48ef-9ccc-85a8602c0438'}, {u'Thumbnail': {u'ContentType': u'image/jpg', u'MediaUrl': u'http://ts1.mm.bing.net/th?id=H.4760392518929204&pid=15.1', u'__metadata': {u'type': u'Bing.Thumbnail'}, u'Height': u'187', u'Width': u'300', u'FileSize': u'12228'}, u'SourceUrl': u'http://www.fanpop.com/clubs/harry-potter/images/33045833/title/harry-potter-wallpaper', u'ContentType': u'image/jpeg', u'MediaUrl': u'http://images6.fanpop.com/image/photos/33000000/Harry-Potter-harry-potter-33045833-1920-1200.jpg', u'__metadata': {u'type': u'ImageResult', u'uri': u"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query='HarryPotter'&$skip=9&$top=1"}, u'DisplayUrl': u'www.fanpop.com/clubs/harry-potter/images/33045833/title/harry...', u'Width': u'1920', u'FileSize': u'1058210', u'Title': u'Harry Potter - Harry Potter Wallpaper (33045833) - Fanpop fanclubs', u'Height': u'1200', u'ID': u'86096d0e-2a5d-40b2-9dee-980e478fe4da'}, {u'Thumbnail': {u'ContentType': u'image/jpg', u'MediaUrl': u'http://ts2.mm.bing.net/th?id=H.4746158975749037&pid=15.1', u'__metadata': {u'type': u'Bing.Thumbnail'}, u'Height': u'225', u'Width': u'300', u'FileSize': u'14017'}, u'SourceUrl': u'http://www.whitegadget.com/pc-wallpapers/141002-harry-potter.html', u'ContentType': u'image/jpeg', u'MediaUrl': u'http://www.whitegadget.com/attachments/pc-wallpapers/73493d1314770623-harry-potter-harry-potter-pictures.jpg', u'__metadata': {u'type': u'ImageResult', u'uri': u"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query='HarryPotter'&$skip=10&$top=1"}, u'DisplayUrl': u'www.whitegadget.com/pc-wallpapers/141002-harry-potter.html', u'Width': u'1024', u'FileSize': u'738226', u'Title': u'73493d1314770623-harry-potter-harry-potter-pictures.jpg', u'Height': u'768', u'ID': u'28b78cb6-d215-4b6c-869f-13c3cd66bd52'}, {u'Thumbnail': {u'ContentType': u'image/jpg', u'MediaUrl': u'http://ts1.mm.bing.net/th?id=H.4743204058630912&pid=15.1', u'__metadata': {u'type': u'Bing.Thumbnail'}, u'Height': u'240', u'Width': u'300', u'FileSize': u'20956'}, u'SourceUrl': u'http://www.fanpop.com/clubs/harry-potter/images/12708172/title/harry-potter', u'ContentType': u'image/jpeg', u'MediaUrl': u'http://images2.fanpop.com/image/photos/12700000/Harry-Potter-harry-potter-12708172-1280-1024.jpg', u'__metadata': {u'type': u'ImageResult', u'uri': u"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query='HarryPotter'&$skip=11&$top=1"}, u'DisplayUrl': u'www.fanpop.com/clubs/harry-potter/images/12708172/title/harry-potter', u'Width': u'1280', u'FileSize': u'417885', u'Title': u'Harry Potter - Harry Potter Wallpaper (12708172) - Fanpop fanclubs', u'Height': u'1024', u'ID': u'3c6f81f3-68bb-4978-bddc-5bf52722d41e'}, {u'Thumbnail': {u'ContentType': u'image/jpg', u'MediaUrl': u'http://ts2.mm.bing.net/th?id=H.4532102163793309&pid=15.1', u'__metadata': {u'type': u'Bing.Thumbnail'}, u'Height': u'225', u'Width': u'300', u'FileSize': u'15970'}, u'SourceUrl': u'http://www.fanpop.com/clubs/harry-potter-and-the-goblet-of-fire/images/1913230/title/harry-potter-cast-photo', u'ContentType': u'image/jpeg', u'MediaUrl': u'http://images1.fanpop.com/images/photos/1900000/Harry-Potter-cast-harry-potter-and-the-goblet-of-fire-1913230-2560-1924.jpg', u'__metadata': {u'type': u'ImageResult', u'uri': u"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query='HarryPotter'&$skip=12&$top=1"}, u'DisplayUrl': u'www.fanpop.com/clubs/harry-potter-and-the-goblet-of-fire/images...', u'Width': u'2560', u'FileSize': u'383178', u'Title': u'Harry Potter cast - Harry Potter & the goblet of fire Photo (1913230 ...', u'Height': u'1924', u'ID': u'bf8b8497-cc90-4179-a69d-0720d652a61c'}, {u'Thumbnail': {u'ContentType': u'image/jpg', u'MediaUrl': u'http://ts2.mm.bing.net/th?id=H.4635232882986213&pid=15.1', u'__metadata': {u'type': u'Bing.Thumbnail'}, u'Height': u'225', u'Width': u'300', u'FileSize': u'6953'}, u'SourceUrl': u'http://www.taringa.net/posts/imagenes/2740577/Imagenes-Harry-Potter.html', u'ContentType': u'image/jpeg', u'MediaUrl': u'http://www.tustrucos.com/wallpapers/Peliculas/fondos-cine-Harry-Potter/Harry-Potter-3.jpg', u'__metadata': {u'type': u'ImageResult', u'uri': u"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Qu

如何从此列表中删除第一张图片的 url 并打印它?谢谢。

4

3 回答 3

0

You want something like:

r.json()['d']['results'][0]['MediaUrl']
于 2013-08-15T04:14:22.273 回答
0

I assume you want to remove the 'url' attribute from the first element.

json = r.json()['d']['results']
del json[0]["Url"]
于 2013-08-15T04:15:12.353 回答
0

如果u'Url'是您想要的密钥,那么:

array[0]["u'Url'"]

于 2013-08-15T04:10:46.997 回答