Python无法从selectrolib导入Extractor?

问题描述 投票:0回答:1

我正在尝试跟进有关webscraping的教程。当我成功编译当我尝试编译网页中列出的代码时,通过GitHub存储库进行Selectorlib:

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

我收到以下错误:

追踪(最近通话):

文件“ test.py”,第1行,在

来自selectorlib导入提取器

[ImportError:无法从'selectorlib'(未知位置)导入名称'Extractor']

什么可能导致此问题?我尝试使用谷歌搜索,仅导入选择器库,从pip安装提取器,仅导入提取器,似乎没有解决此问题的方法,从教程发行版到现在有什么变化吗?

编辑:重新安装选择器库后,我得到一个不同的错误:

来自selectorlib导入提取器

文件“ /usr/local/lib/python2.7/dist-packages/selectorlib/init.py”,第9行,在

从.selectorlib导入提取器#noqa:F401

文件“ /usr/local/lib/python2.7/dist-packages/selectorlib/selectorlib.py”,第33行

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

错误指示在:yaml_string:我在Raspberry Buster(RPI 3B +)上运行python]

我正在尝试跟进有关网络抓取的教程。当我通过GitHub存储库成功编译Selectorlib时,当我尝试编译网页中列出的代码时:from selectorlib import ...

python web-scraping
1个回答
1
投票

[我尝试使用pip install selectorlib,并且导入Extractor类对我来说很好。您可能需要执行以下步骤来解决此问题

© www.soinside.com 2019 - 2024. All rights reserved.