0

I have made a webscraper in python and I am now trying to get it to output the data i have scraped into a mysqldb but I keep getting this error and I don't know how to fix it.this is the error I get

 File "C:/Users/owner/Downloads/Scrape (2).py", line 16, in <module>
    cursor.execute("INSERT INTO london10dayforecast (Day,Condition,High,Low,) VALUES (?,?,?,?)", str(var1),str(var2),str(var3),str(var4))
TypeError: execute() takes at most 3 arguments (6 given)

Process finished with exit code 1

Here is the code:

import requests
import MySQLdb

r = requests.get("http://api.wunderground.com/api/be6eb0a1b404829b/forecast10day/q/UK/London.json")
data = r.json()

for day in data['forecast']['simpleforecast']['forecastday']:
var1= day['date']['weekday'] + ":"
var2= "Conditions: ", day['conditions']
var3= "High: ", day['high']['celsius'] + "'C"
var4= "Low: ", day['low']['celsius'] + "'C", '\n'

db = MySQLdb.connect("127.0.0.1","root","","weathersystem" )
cursor = db.cursor()

cursor.execute("INSERT INTO london10dayforecast (Day,Condition,High,Low,) VALUES (?,?,?,?)", str(var1),str(var2),str(var3),str(var4))
db.commit()
db.close()

``

1 Answer 1

1

You need to pass the values in as a tuple

cursor.execute("INSERT INTO london10dayforecast (Day,Condition,High,Low) VALUES (%s,%s,%s,%s)", (str(var1),str(var2),str(var3),str(var4)))

or more readably

qstr = "INSERT INTO london10dayforecast (Day,Condition,High,Low) VALUES (%s,%s,%s,%s)"
params = str(var1), str(var2), str(var3), str(var4)
cursor.execute(qstr, params)
Sign up to request clarification or add additional context in comments.

7 Comments

Is it still the same error as in question? If not what is new error?
i noticed, even with the correction i still get this error Traceback (most recent call last): File "C:/Users/owner/Downloads/Scrape (2).py", line 17, in <module> cursor.execute(qstr, params) File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 187, in execute query = query % tuple([db.literal(item) for item in args]) TypeError: not all arguments converted during string formatting
@RitchieRamnial, use %s instead of ? in the query string
There is an extra comma next to Low. That might cause a problem. Not sure for MySQLdb but I'm sure it causes a problem in sqlite3.
@RitchieRamnial, still the TypeError: not all arguments converted...?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.