Python爬虫集

这里是最近几年写过的爬虫,做一个分享

1. NASA Mars图片爬虫

NASA的图片是可以通过接口获取到,这就很简单,我们只需要访问接口去获取就行了


程序运行截图

爬取的图片
import requests
import json

headers = {
    "user-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
}

params = {
    'size':100,
    'from':100    ,
    'sort':'promo-date-time:desc',
    'q':'((ubernode-type:image) AND (topics:3152))',
    '_source_include':'promo-date-time,master-image,nid,title,topics,missions,collections,other-tags,ubernode-type,primary-tag,secondary-tag,cardfeed-title,type,collection-asset-link,link-or-attachment,pr-leader-sentence,image-feature-caption,attachments,uri'
}
# https://www.nasa.gov/sites/default/files/thumbnails/image/pia23454.jpg

public_path = "https://www.nasa.gov/sites/default/files/"

rsp=requests.get('https://www.nasa.gov/api/2/ubernode/_search',headers=headers,params=params)
# print(rsp.text)
b = json.loads  (rsp.text)
hits = b['hits']['hits']
for h in hits:
    _source = h['_source']
    img_path = _source['master-image']['uri']
    title = _source['title'].replace(' ',"_").replace("'","").replace("/","-")
    img_path = img_path.replace('public://',public_path)
    print(img_path)
    try:

        r=requests.get(img_path,stream=True,headers=headers,timeout=10)
        f=open('./img/%s.jpg'%title,'wb')
        for a in r.iter_content(chunk_size=1024):
            f.write(a)
        f.close()
        print("Downloaded: "+title)
    except:
        print("Download failed: "+title)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 2018年3月11日,正值全国“两会”召开期间在北京值班之际,在国家大剧院观看了人生的第一场舞剧……《李白》,完成...
    桃成蹊_a0f2阅读 781评论 0 0
  • 信息 一日有昼夜, 思念无分别。 遥遥沧海意, 鸿雁飞不绝。 2018年11月24日
    深圳凡夫阅读 220评论 0 0
  • 今天午餐是我做的,老公说青菜炒得真漂亮,其实青菜是烫的,没有炒,确实很漂亮,看起来像炒的,又绿油油的,可惜没有拍照...
    顾璟小花阅读 222评论 0 0
  • 我一直有个梦想能上讲台,为了离梦想进一点,今天交了1万多元钱成了合伙人,其实我也不知道合伙人是做什么?我只知道可以...
    北北妈阅读 89评论 0 1