By Tom Chen via Discussions on Python.org at 20Sep2022 09:08:
I copied the same codes from
Web Scraping with Python – How to Scrape Data from Twitter using Tweepy and Snscrape, and
ran them in Visual Studio or Python 3.10. Nothing works. I always get
“Syntax Error: invalid syntax”.
There is a clue at your Python prompt. Observe:
>>> import snscrape.modules.twitter as sntwitter
>>> import pandas as pd
>>>
>>> attributes_container = []
>>>
>>> for i,tweet in enumerate(sntwitter.TwitterSearchScraper('sex for grades since:2021-07-05 until:2022-07-06').get_items()):
... if i>150:
... break
... attributes_container.append([tweet.user.username, tweet.date, tweet.likeCount, tweet.sourceLabel, tweet.content])
...
... tweets_df = pd.DataFrame(attributes_container, columns=["User", "Date Created", "Number of Likes", "Source of Tweet", "Tweet"])
File "<stdin>", line 6
tweets_df = pd.DataFrame(attributes_container, columns=["User", "Date Created", "Number of Likes", "Source of Tweet", "Tweet"])
^^^^^^^^^
SyntaxError: invalid syntax
When you go to type tweets_df=......
, the prompt is still ...
, which
indicates that the prompt still thinks your for-loop is incomplete.
Because of that, it expects the tweets_df=....
to be part of the loop
body, and so it should be indents like the if-statement.
However, it isn’t intended as part of the loop body.
My belief is that you used copy/paste to give this code to the
interactive prompt, and that the copied text had some whitespace
(probably just spaces) in the blank line. Here’s me trying to reproduce
this:
>>> for x in 1, 2, 3:
... print(x)
...
... foo
File "<stdin>", line 4
foo
^
SyntaxError: invalid syntax
It isn’t visible, but I deliberately typed a couple of space on the
blank line.
The interactive prompt does not behave exactly like a normal Python
script. Because it tries to gather up things as you type them so that
it can run them immediately, it uses some little cues to decide whether
you’ve typed something which needs one line or several lines.
If you type some spaces on a blank line, it decides there’s more code to
come, which is probably what caused your SyntaxError
.
Try copy/pasting the for-loop but not the following blank line. Then
press Enter/Return to end the loop (the prompt needs that little cue to
decide the loop is complete). Then type or copy the tweets_df=
assignment line.
Most of us write code in files, and run the file. For example we might
put your little programme there in a file called get_tweets.py
, and
run it as:
python get_tweets.py
This avoids the foibles of the interactive prompt, and also means you
get to edit the file to make changes and retry instead of having to
retype the whole thing again from scratch.
Cheers,
Cameron Simpson cs@cskk.id.au