vendredi 17 juin 2016

Shell Code executes directly, but not in PythonScript

I wrote a little Python function that uses the BeatifulSoup libary. The function should return a list of symbols from a Wikipedia article.

In the Shell, I execute the function like this:

pythonScript.my_function()

...it throws a error in line 28:

No connections adapters were found for 'link'.

When I type the same code from my function directly in the shell, it works perfectly. With the same link. I have even copied and pasted the lines.

These are the two lines of code I'm talking about, the error appears with the BeautifulSoup function.

response = requests.get('link')
soup = bs4.BeautifulSoup(response.text)

I can't explain why this error happens...

EDIT: here is the full code

#/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)

I think that must be enough. This is the point where the error occurs. In the Shell Ive done everthing step by step: importing the libaries and then calling the requests and bs4 function. The single difference is that in the Shell I didnt defined a function for that.

EDIT2:

Here is the exact Error Message:

Traceback (most recent call last): File "", line 1, in File "/home/felix/Dokumente/PythonSAT/DownloadSymbolsFromWikipedia.py", line 28, in obtain_parse_wiki_snp500

soup = bs4.BeautifulSoup(response.text) File "/home/felix/anaconda3/lib/python3.5/site-packages/requests/api.py", line 67, in get return request('get', url, params=params, **kwargs)

File "/home/felix/anaconda3/lib/python3.5/site-packages/requests/api.py", line 53, in request return session.request(method=method, url=url, **kwargs) File "/home/felix/anaconda3/lib/python3.5/site-packages/requests/sessions.py", line 468, in request resp = self.send(prep, **send_kwargs)

File "/home/felix/anaconda3/lib/python3.5/site-packages/requests/sessions.py", line 570, in send adapter = self.get_adapter(url=request.url)

File "/home/felix/anaconda3/lib/python3.5/site-packages/requests/sessions.py", line 644, in get_adapter raise InvalidSchema("No connection adapters were found for '%s'" % url) requests.exceptions.InvalidSchema: No connection adapters were found for 'htttp://en.wikipedia.org/wiki/list_of_S%26P_500_companies'

Aucun commentaire:

Enregistrer un commentaire