Jupyter中使用非Anaconda模块new-crawler遇语法错误求助
Hey there, let's break down why you're hitting that SyntaxError and fix it step by step:
1. First, fix the module import
Your line %aimport news-crawler as nc has a critical issue:
- Python doesn't allow hyphens (
-) in module names. Even though you installed the package asnew-crawler, when importing it in Python, you need to use underscores (_) instead. So the correct import line is:import news_crawler as nc - Also,
%aimportis Jupyter's auto-reload magic command—you only need that if you're actively editing the module and want changes to load automatically. For a standard first import, plainimportworks just fine.
2. You're mixing command-line syntax with Python code
The line nc reports USA -o out.csv -s 20160101 -e 20161231 -keyword election is how you'd run the tool in a terminal, not in a Python script/Jupyter cell. In Python, you need to call the actual function from the module and pass arguments using Python's syntax.
Assuming the news_crawler module has a reports function (which makes sense given your command), you'll need to pass parameters as keyword arguments like this:
nc.reports( country="USA", output="out.csv", start_date="20160101", end_date="20161231", keyword="election" )
If you're not sure about the exact parameter names, you can quickly check the function's documentation with:
help(nc.reports)
Or list all available functions in the module to confirm:
dir(nc)
Alternative: Run it as a command-line command in Jupyter
If you prefer using the terminal-style syntax, you can prefix the command with ! to run it directly in a Jupyter cell (this works as long as your Anaconda environment is properly set up):
!news-crawler reports USA -o out.csv -s 20160101 -e 20161231 -keyword election
Either approach should get rid of that SyntaxError and let you run your crawler successfully.
内容的提问来源于stack exchange,提问作者user1150393

