python没有架构_python – 没有提供架构和使用requests.get()的其他错误

我正在通过自动化无聊的东西来学习Python.该程序应该转到

http://xkcd.com/并下载所有图像以供离线查看.

我使用的是2.7版和Mac版.

出于某种原因,我收到的错误如“没有提供架构”和使用request.get()本身的错误.

这是我的代码:

# Saves the XKCD comic page for offline read

import requests, os, bs4, shutil

url = 'http://xkcd.com/'

if os.path.isdir('xkcd') == True: # If xkcd folder already exists

shutil.rmtree('xkcd') # delete it

else: # otherwise

os.makedirs('xkcd') # Creates xkcd foulder.

while not url.endswith('#'): # If there are no more posts, it url will endswith #, exist while loop

# Download the page

print 'Downloading %s page...' % url

res = requests.get(url) # Get the page

res.raise_for_status() # Check for errors

soup = bs4.BeautifulSoup(res.text) # Dowload the page

# Find the URL of the comic image

comicElem = soup.select('#comic img') # Any #comic img it finds will be saved as a list in comicElem

if comicElem == []: # if the list is empty

print 'Couldn\'t find the image!'

else:

comicUrl = comicElem[0].get('src') # Get the first index in comicElem (the image) and save to

# comicUrl

# Download the image

print 'Downloading the %s image...' % (comicUrl)

res = requests.get(comicUrl) # Get the image. Getting something will always use requests.get()

res.raise_for_status() # Check for errors

# Save image to ./xkcd

imageFile = open(os.path.join('xkcd', os.path.basename(comicUrl)), 'wb')

for chunk in res.iter_content(10000):

imageFile.write(chunk)

imageFile.close()

# Get the Prev btn's URL

prevLink = soup.select('a[rel="prev"]')[0]

# The Previous button is first < Prev

url = 'http://xkcd.com/' + prevLink.get('href')

# adds /1535/ to http://xkcd.com/

print 'Done!'

以下是错误:

Traceback (most recent call last):

File "/Users/XKCD.py", line 30, in

res = requests.get(comicUrl) # Get the image. Getting something will always use requests.get()

File "/Library/Python/2.7/site-packages/requests/api.py", line 69, in get

return request('get', url, params=params, **kwargs)

File "/Library/Python/2.7/site-packages/requests/api.py", line 50, in request

response = session.request(method=method, url=url, **kwargs)

File "/Library/Python/2.7/site-packages/requests/sessions.py", line 451, in request

prep = self.prepare_request(req)

File "/Library/Python/2.7/site-packages/requests/sessions.py", line 382, in prepare_request

hooks=merge_hooks(request.hooks, self.hooks),

File "/Library/Python/2.7/site-packages/requests/models.py", line 304, in prepare

self.prepare_url(url, params)

File "/Library/Python/2.7/site-packages/requests/models.py", line 362, in prepare_url

to_native_string(url, 'utf8')))

requests.exceptions.MissingSchema: Invalid URL '//imgs.xkcd.com/comics/the_martian.png': No schema supplied. Perhaps you meant http:imgs.xkcd.com/comics/the_martian.png?

问题是我一直在阅读书中关于该程序的部分多次,阅读请求文档,以及在这里查看其他问题.我的语法看起来正确.

谢谢你的帮助!

编辑:

这不起作用:comicUrl =(“http:”comicElem [0] .get(‘src’))我以为添加http:之前会摆脱没有架构提供的错误.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值