You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
for li in img_li:
imgs = li.find_all('img')
for img in imgs:
url = img['src']
r = requests.get(url, stream=True)
image_name = url.split('/')[-1]
with open('./img/%s' % image_name, 'wb') as f:
for chunk in r.iter_content(chunk_size=128):
f.write(chunk)
print('Saved %s' % image_name)
The text was updated successfully, but these errors were encountered:
这是运行后报错的代码
Traceback (most recent call last):
File "3-3-practice-download-images.py", line 16, in
r = requests.get(url, stream=True)
File "C:\PYTHON\lib\site-packages\requests\api.py", line 76, in get
return request('get', url, params=params, **kwargs)
File "C:\PYTHON\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\PYTHON\lib\site-packages\requests\sessions.py", line 516, in request
prep = self.prepare_request(req)
File "C:\PYTHON\lib\site-packages\requests\sessions.py", line 449, in prepare_request
p.prepare(
File "C:\PYTHON\lib\site-packages\requests\models.py", line 314, in prepare
self.prepare_url(url, params)
File "C:\PYTHON\lib\site-packages\requests\models.py", line 388, in prepare_url
raise MissingSchema(error)
requests.exceptions.MissingSchema: Invalid URL '': No schema supplied. Perhaps you meant http://?
from bs4 import BeautifulSoup
import requests
import os
os.makedirs('./img/', exist_ok=True)
URL = "http://www.nationalgeographic.com.cn/animals/"
html = requests.get(URL).text
soup = BeautifulSoup(html, 'lxml')
img_li = soup.find_all('li', {"class": "mod_w"})
for li in img_li:
imgs = li.find_all('img')
for img in imgs:
url = img['src']
r = requests.get(url, stream=True)
image_name = url.split('/')[-1]
with open('./img/%s' % image_name, 'wb') as f:
for chunk in r.iter_content(chunk_size=128):
f.write(chunk)
print('Saved %s' % image_name)
The text was updated successfully, but these errors were encountered: