Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

爬取地理网站的图片报错raise MissingSchema(error) requests.exceptions.MissingSchema: Invalid URL '': No schema supplied. Perhaps you meant http://? #327

Open
whiteminta3 opened this issue Oct 25, 2020 · 1 comment

Comments

@whiteminta3
Copy link

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)

@whiteminta3
Copy link
Author

这是运行后报错的代码
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://?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant