I am new to Python and am getting this error:
Traceback (most recent call last):
File \"/usr/local/bin/scrapy\", line 4, in
execute()
While the indentation errors are obvious in the StackOverflow page, they may not be in your editor. You have a mix of different indentation types here, 1, 4 and 8 spaces. You should always use four spaces for indentation, as per PEP8. You should also avoid mixing tabs and spaces.
I also recommend that you try to run your script using the '-tt' command-line option to determine when you accidentally mix tabs and spaces. Of course any decent editor will be able to highlight tabs versus spaces (such as Vim's 'list' option).
As the error says you have not correctly indented code, check_exists_sql
is not aligned with line above it cursor = db.cursor()
.
Also use 4 spaces for indentation.
Read this http://diveintopython.net/getting_to_know_python/indenting_code.html
The error is pretty straightforward - the line starting with check_exists_sql
isn't indented properly. From the context of your code, I'd indent it and the following lines to match the line before it:
#open db connection
db = MySQLdb.connect("localhost","root","str0ng","TESTDB")
#prepare a cursor object using cursor() method
cursor = db.cursor()
#see if any links in the DB match the crawled link
check_exists_sql = "SELECT * FROM LINKS WHERE link = '%s' LIMIT 1" % item['link']
cursor.execute(check_exists_sql)
And keep indenting it until the for
loop ends (all the way through to and including items.append(item)
.
The indentation is wrong, as the error tells you. As you can see, you have indented the code beginning with the indicated line too little to be in the for
loop, but too much to be at the same level as the for loop. Python sees the lack of indentation as ending the for
loop, then complains you have indented the rest of the code too much. (The def
line I'm betting is just an artifact of how Stack Overflow wants you to format your code.)
Edit: Given your correction, I'm betting you have a mixture of tabs and spaces in the source file, such that it looks to the human eye like the code lines up, but Python considers it not to. As others have suggested, using only spaces is the recommended practice (see PEP 8). If you start Python with python -t
, you will get warnings if there are mixed tabs and spaces in your code, which should help you pinpoint the issue.
This error occur when you don't correctly write blocks. Forgetting a ":", or not using "Tab" button for blocks and use spaces. When you are transporting a code from one editor to another editor,it can happen. And never forget this: errors aren't always on that line. I came here for this, but I've forgotten an except after a try. because of my unstandard editor, it happend. But it's possible in normal editor.
import urllib.request
import requests
from bs4 import BeautifulSoup
r = requests.get('https://icons8.com/icons/set/favicon')
If you try to connect to such a site, you will get an indent error.
import urllib.request
import requests
from bs4 import BeautifulSoup
r = requests.get('https://icons8.com/icons/set/favicon')
Python cares about indents