【问题标题】:Shell Code executes directly, but not in PythonScriptShell 代码直接执行,但不在 PythonScript 中
【发布时间】: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


【解决方案1】:

你已经传递了一个带有htttp 的链接,即你有三个 t 而应该只有两个,无论你在哪里运行该代码都会出错。正确的网址是:

http://en.wikipedia.org/wiki/list_of_S%26P_500_companies

一旦你修复了 url,请求就可以正常工作:

n [1]: import requests


In [2]: requests.get('http://en.wikipedia.org/wiki/list_of_S%26P_500_companies')
Out[3]: <Response [200]>

获取符号:

n [28]: for tr in soup.select("table.wikitable.sortable tr + tr"):
             sym = tr.select_one("a.external.text")
             if sym:
                print(sym.text)
   ....:         
MMM
ABT
ABBV
ACN
ATVI
AYI
ADBE
AAP
AES
AET
AFL
AMG
A
GAS
APD
AKAM
ALK
AA
AGN
ALXN
ALLE
ADS
ALL
GOOGL
GOOG
MO
AMZN
AEE
AAL
AEP

等等……

【讨论】:

  • 我已经检查过了,但在我的脚本中没有空格.. 将它粘贴到 stackOverflow 中时一定出错了。
  • 在堆栈溢出编辑器中,链接被分开,因为它太长了。或许正因为如此,空间才出现。
  • @feliz,没有htttp协议它是http
  • 我已经找到了。由于我脚本中的链接没有三重t..一定是另一个错误
  • 不,你 100% 的脚本中有错误的 URL。确保您按照自己的想法运行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-29
  • 2019-11-27
相关资源
最近更新 更多