python 抓取圖片

來源:互聯網
上載者:User

標籤:ref   git   抓取   +=   path   csv   post   current   arm   

****

# -*- coding:utf-8 -*-
from urllib import request
from bs4 import BeautifulSoup
import re
import time

url = "https://www.zhihu.com/question/22918070"
html = request.urlopen(url).read().decode(‘utf-8‘)
soup = BeautifulSoup(html,‘html.parser‘)
#print(soup.prettify())

#用Beautiful Soup結合Regex來提取包含所有圖片連結(img標籤中,class=**,以.jpg結尾的連結)的語句
links = soup.find_all(‘img‘, "origin_image zh-lightbox-thumb",src=re.compile(r‘.jpg$‘))
print(links)

# 設定儲存圖片的路徑,否則會儲存到程式當前路徑
path = r‘/home/kong/PycharmProjects/untitled2/image/‘ #路徑前的r是保持字串原始值的意思,就是說不對其中的符號進行轉義
for link in links:
print(link.attrs[‘src‘])
#儲存連結並命名,time.time()返回目前時間戳防止命名衝突
request.urlretrieve(link.attrs[‘src‘],path+‘\%s.jpg‘ % time.time()) #使用request.urlretrieve直接將所有遠程連結資料下載到本地

 

*****

import csv
import requests
import re
import urllib
from collections import namedtuple
from lxml import etree
from bs4 import BeautifulSoup

 

def schedule(blocknum, blocksize, totalsize):
"""
blocknum: 123
blocksize: 456
totalsize: 789
"""
per = 100.0 * blocknum * blocksize / totalsize

if per > 100:
per = 100

print "current download schedule: %d" % per


user_agent = ‘Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:35.0) Gecko/20100101 Firefox/35.0‘
headers = {‘User-Agent‘: user_agent}
r = requests.get(‘http://www.win4000.com/wallpaper_detail_118605.html‘, headers=headers)
# r = requests.get(‘http://www.ivsky.com/tupian/ziranfengguang/‘, headers=headers)
html = etree.HTML(r.text)


# u = html.xpath(‘.//*[@class="imgitem"]‘)

img_urls = html.xpath(‘.//img/@src‘)
# img_urls = html.xpath(‘.//img/@src‘)
i = 0
for img_url in img_urls:
urllib.urlretrieve(img_url, ‘img‘+str(i) + ‘.jpg‘, schedule)
i += 1

 

python 抓取圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.