美文网首页
初试爬虫-爬取图片

初试爬虫-爬取图片

作者: Mr希灵 | 来源:发表于2016-03-10 17:08 被阅读131次

    采用requests库和beautiful soup

    import requests
    from bs4 import BeautifulSoup
    
    # 站点信息
    weburl = r"http://www.meizitu.com"
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)
     Chrome/49.0.2623.75 Safari/537.36'}
    page = requests.get(weburl,headers = headers)
    soup = BeautifulSoup(page.text,"html.parser")
    
    # 保存图片
    num_img = 0
    for link in soup.find_all('img'):
        img_url = link.get('src')
        r = requests.get(img_url)
        num_img += 1
        img_name = 'd:\pic\meizitu' + str(num_img) + '.jpg'
        with open(img_name,'wb') as f:
            f.write(r.content)
        print('picture ' + str(num_img))
    
    print('图片下载完成')
    

    相关文章

      网友评论

          本文标题:初试爬虫-爬取图片

          本文链接:https://www.haomeiwen.com/subject/mhmtlttx.html