我正在努力跟进一个关于网刮的教程。虽然我通过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’
是什么导致了这个问题?我试着谷歌它,只导入selectorlib,从pip中安装提取器,只导入extractor,似乎什么都解决不了这个问题,从教程发布到现在,有什么改变吗?
编辑:重新安装selectorlib之后,我得到一个不同的错误:
从selectorlib导入抽取器 文件"/usr/local/lib/python2.7/dist-packages/selectorlib/init.py",第9行,在 从.selectorlib进口抽油器#noqa:F 401 文件"/usr/local/lib/python2.7/dist-packages/selectorlib/selectorlib.py",第33行 def from_yaml_string(cls,yaml_string: str,formatters=None):
错误指示为at:yaml_string:,我正在Raspberry Buster上运行python (RPI 3B+)
发布于 2019-11-25 05:14:51
我试过使用pip install selectorlib,导入Extractor类对我来说很好。您可能需要执行以下步骤来消除此问题
python setup.py install --record install_files.txtxargs rm -rf < install_files.txt或Windows使用powershell命令Get-Content install_files.txt | ForEach-Object {Remove-Item $_ -Recurse -Force}pip install selectorlib安装软件包希望这应该清除问题,因为它新安装的帮助轮文件包。
https://stackoverflow.com/questions/59018792
复制相似问题