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+)