python抓取网页上图片

Python018

python抓取网页上图片,第1张

正则表达式匹配的url有错误

for x in add:

print x # 这里可以看到报错的时候是 url 错误

dirpath = os.path.join('C:\\Users\\lilinan\\Desktop\\新建文件夹','%s.jpg' % t)

urllib.request.urlretrieve(x,dirpath)

t+=1

import urllib.request

import ssl

from bs4 import BeautifulSoup

import lxml

ssl._create_default_https_context = ssl._create_unverified_context

url = "https://app.griffith.edu.au/explore-student-blog/what-do-you-order-at-an-australian-cafe/"

response = urllib.request.urlopen(url)

html = response.read()

soup = BeautifulSoup(html, 'lxml')

res = soup.find('div', class_='post-entry').find_all('a')[10]

result = res.find('img')['src']

print(result)

filename = 'photo' + '.jpg'

f = open(filename,'w')

urllib.request.urlretrieve(result, filename)