【发布时间】:2016-10-18 12:29:59
【问题描述】:
我编写了一个使用 BeatifulSoup 库的小 Python 函数。该函数应返回 Wikipedia 文章中的符号列表。
在Shell中,我执行如下函数:
pythonScript.my_function()
...它在第 28 行抛出错误:
No connections adapters were found for 'link'.
当我直接在 shell 中从我的函数中键入相同的代码时,它可以完美运行。使用相同的链接。我什至复制并粘贴了这些行。
这是我说的这两行代码,错误出现在BeautifulSoup函数中。
response = requests.get('link')
soup = bs4.BeautifulSoup(response.text)
我无法解释为什么会发生这个错误...
编辑:这里是完整的代码
#/usr/bin/python
# -*- coding: utf-8 -*-
#insert_symbols.py
from __future__ import print_function
import datetime
from math import ceil
import bs4
import MySQLdb as mdb
import requests
def obtain_parse_wiki_snp500():
'''
Download and parse the Wikipedia list of S&P500
constituents using requests and BeatifulSoup.
Returns a list of tuples for to add to MySQL.
'''
#Stores the current time, for the created at record
now = datetime.datetime.utcnow()
#Use requests and BeautifulSoup to download the
#list of S&P500 companies and obtain the symbol table
response = requests.get('http://en.wikipedia.org/wiki/list_of_S%26P_500_companies')
soup = bs4.BeautifulSoup(response.text)
我认为这已经足够了。这是发生错误的地方。 在 Shell 中,我一步一步完成了所有工作: 导入库,然后调用 requests 和 bs4 函数。 唯一的区别是在 Shell 中我没有为此定义函数。
EDIT2:
这是确切的错误消息:
Traceback(最近一次调用最后一次): 文件“”,第 1 行,在 文件“/home/felix/Dokumente/PythonSAT/DownloadSymbolsFromWikipedia.py”,第 28 行,在 gain_parse_wiki_snp500 中
汤 = bs4.BeautifulSoup(response.text) 文件“/home/felix/anaconda3/lib/python3.5/site-packages/requests/api.py”,第 67 行,在 get 返回请求('get', url, params=params, **kwargs)
文件“/home/felix/anaconda3/lib/python3.5/site-packages/requests/api.py”,第 53 行,在请求中 返回 session.request(method=method, url=url, **kwargs) 文件“/home/felix/anaconda3/lib/python3.5/site-packages/requests/sessions.py”,第 468 行,在请求中 resp = self.send(prep, **send_kwargs)
文件“/home/felix/anaconda3/lib/python3.5/site-packages/requests/sessions.py”,第 570 行,在发送中 适配器 = self.get_adapter(url=request.url)
文件“/home/felix/anaconda3/lib/python3.5/site-packages/requests/sessions.py”,第 644 行,在 get_adapter raise InvalidSchema("没有为 '%s' 找到连接适配器" % url) requests.exceptions.InvalidSchema:未找到“htttp://en.wikipedia.org/wiki/list_of_S%26P_500_companies”的连接适配器
【问题讨论】:
-
请贴出你的实际代码,足以让你真正看到问题所在。
标签: python function shell beautifulsoup bs4