Bonjour,

Je suis actuellement entrain de faire un parser avec BeautifulSoup qui marche bien et j'essaie donc d'appeler ma fonction Python depuis mon programme C. J'ai fais juste un premier morceau pour tester au fur et à mesure mais lorsque je fais PyObject* Function = PyObject_GetAttrString(Module, (char*)"link_list") je récupère l'erreur que j'ai mentionné dans le titre. link_list existe bel et bien dans mon fichier Python et voici mon programme :

pars.c

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#include <Python.h>
 
int main() {
  Py_Initialize();
 
  /* 1st: Import the module */
    PyObject* ModuleString = PyString_FromString((char*) "parser");
    if (!ModuleString) {
        PyErr_Print();
        printf("Error formating python script\n");
    }
 
    PyObject* Module = PyImport_Import(ModuleString);
    if (!Module) {
        PyErr_Print();
        printf("Error importing python script\n");
    }
 
    /* 2nd: Getting reference to the function */
    PyObject* Function = PyObject_GetAttrString(Module, (char*)"link_list");
    if (!Function) {
        PyErr_Print();
    }
 
    Py_Finalize();
    return 0;
}
parser.py

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/local/bin/python2.7
from bs4 import BeautifulSoup
import urllib2
 
 
def link_list(urlString):
    siteFile = urllib2.urlopen(urlString)
    siteHTML = siteFile.read()
    siteFile.close()
    soup = BeautifulSoup(siteHTML, "html.parser")
    liste = []
    for links in soup.find_all('a'):
        print(links.get('href'))
        liste.append(links.get('href'))
    return liste
Je ne comprends pas pourquoi il de trouve pas l'attribut. Pour plus d'information avant j'avais mis un PySys_SetPath(".") qui engendrait un problème lors de l'importation des bibliothèques. Dans mon répertoire de travail il y a __init__.py parser.py et pars.c ma méthode de compilation se fait avec gcc -I/usr/include/python2.7 pars.c -lpython2.7 -o pars -Wall. J'ai bien regardé le doc sans grand succès ...