0
votes

I am trying to follow up a tutorial on webscraping. And while I successfully compiled Selectorlib via GitHub repo, when I try to compile the code listed in the webpage:

from selectorlib import Extractor
import requests 
import json 
import argparseargparser = argparse.ArgumentParser()
argparser.add_argument('url', help='Amazon Product Details URL')# Create an Extractor by reading     from the YAML file
e = Extractor.from_yaml_file('selectors.yml')user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246'
headers = {'User-Agent': user_agent}# Download the page using requests
args = argparser.parse_args()
r = requests.get(args.url, headers=headers)
# Pass the HTML of the page and create 
data = e.extract(r.text)
# Print the data 
print(json.dumps(data, indent=True))

i get the following error:

Traceback (most recent call last):

File "test.py", line 1, in

from selectorlib import Extractor

ImportError: cannot import name 'Extractor' from 'selectorlib' (unknown location)

What could cause this problem? I tried googling it, importing only selectorlib , installing extractor from pip, importing only exctractor, nothing seems to fix this issue, did something changed from the tutorial release to now?

EDIT: After reinstalling selectorlib i get a diffrent error:

from selectorlib import Extractor

File "/usr/local/lib/python2.7/dist-packages/selectorlib/init.py", line 9, in

from .selectorlib import Extractor # noqa:F401

File "/usr/local/lib/python2.7/dist-packages/selectorlib/selectorlib.py", line 33

def from_yaml_string(cls, yaml_string: str, formatters=None):

The error is indicating at: yaml_string: I am running python on Raspberry Buster (RPI 3B+)

1
Which command did you use to install selectorlib? - Asnim P Ansari

1 Answers

1
votes

I have tried using pip install selectorlib and import of Extractor class works fine for me. You might want to execute the following steps to get rid off this issue

  1. Run again the installation command like this python setup.py install --record install_files.txt
  2. For removal of package, if you are using Linux use xargs rm -rf < install_files.txt or Windows use powershell command Get-Content install_files.txt | ForEach-Object {Remove-Item $_ -Recurse -Force}
  3. Try installing the package again with command pip install selectorlib

Hope this should clear the issue as it fresh installs the package with help of wheel files.