python爬取网页图片

from bs4 import  BeautifulSoup
import urllib.request
import urllib

def pachong(url):
    headers = {'User-Agent':
                   'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'}
    req = urllib.request.urlopen(url)#创建对象

    con = req.read()
    soup = BeautifulSoup(con,'html.parser')
    print(con)
    mygirl = soup.find_all('img')
    for girl in mygirl:
        link = girl.get('src')
        print(link)
        global x
        urllib.request.urlretrieve(link,'image\%s.jpg'%x)
        x = x+1
        print("正在下载%s张"%x)


url=('https://www.dbmeinv.com/?pager_offset=2')
x=0
for i in range(5):
    pachong('https://www.dbmeinv.com/?pager_offset=%s'%i)




# soup = BeautifulSoup(open('test.html'),'html.parser')
# # print(soup.prettify())
# html ='我是科比'
# soup = BeautifulSoup(html,'html.parser')
#print(soup.title)
#soup.prettify() 打印所有内容

#https://www.dbmeinv.com/?pager_offset=2
#'https://www.dbmeinv.com/?pager_offset={}'.format();
#User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36

你可能感兴趣的:(python爬取网页图片)