Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #61820 > unrolled thread
| Started by | Jai <jaiprakashsingh213@gmail.com> |
|---|---|
| First post | 2013-12-13 05:18 -0800 |
| Last post | 2013-12-13 14:33 +0100 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
TypeError: not all arguments converted during string formatting Jai <jaiprakashsingh213@gmail.com> - 2013-12-13 05:18 -0800
Re: TypeError: not all arguments converted during string formatting Ervin Hegedüs <airween@gmail.com> - 2013-12-13 14:26 +0100
Re: TypeError: not all arguments converted during string formatting Peter Otten <__peter__@web.de> - 2013-12-13 14:33 +0100
| From | Jai <jaiprakashsingh213@gmail.com> |
|---|---|
| Date | 2013-12-13 05:18 -0800 |
| Subject | TypeError: not all arguments converted during string formatting |
| Message-ID | <01721890-9bbb-4c90-9668-11a73ef5ea8a@googlegroups.com> |
my code :
#!/usr/bin/env python
from bs4 import BeautifulSoup
import re,urllib2,urlparse, MySQLdb
def get_domain(url):
return urlparse.urlparse(url).netloc
def men_tshirts2(main_link, cat_link,db,cursor):
#print main_link
for cat,link in cat_link.iteritems():
cat = str(cat)
#print cat, link
page = urllib2.urlopen(link)
soup = BeautifulSoup(page)
page.close()
item = soup.find_all("div",attrs={"class":"itemTitle"})
price = soup.find_all("div", attrs={"class":"itemPrice"})
item_list =[]
price_list =[]
seller_list =[]
for x,y in zip(item,price):
item_content=str(x.a.string)
price = str(y.p.string)
link = str(x.a.get("href"))
page =urllib2.urlopen(link)
soup = BeautifulSoup(page)
page.close()
data = soup.find_all("span", attrs={"class":"mbg-nw"})
seller = str(data[0].string)
#print cat,item_content,price,seller
gender = "men"
sql = """insert into fashion(GENDER,links,category,item_content,price,seller) VAlUES('%s','%s','%s','%s','%s','s')"""
cursor.execute(sql,(gender,main_link,cat,item_content,price,seller))
db.commit()
#except:
# db.rollback()
#print len(gender),len(main_link),len(cat),len(item_content),len(price),len(seller)
def men_tshirts(db,cursor):
main_link = "http://fashion.ebay.in/index.html#men_tshirts"
domane = get_domain(main_link)
main_page = urllib2.urlopen(main_link)
main_soup=BeautifulSoup(main_page)
main_page.close()
data = main_soup.find_all("div",attrs= {"class":"itmTitle"})
price = main_soup.find_all("span",attrs={"class":"catlblTitle"})
cat_link = {}
for x, y in zip(data, price):
#cat= str(x.a.string)+":"+str(y.string)
cat= str(x.a.string)
link= "http://"+domane+"/"+str(x.a.get("href"))
#print cat, link
cat_link[cat] = link
men_tshirts2(main_link, cat_link,db,cursor)
if __name__=="__main__":
db = MySQLdb.connect("localhost","root","india123","ebay_db" )
cursor = db.cursor()
men_tshirts(db,cursor)
db.close()
++++++++++++++++++++++++++++++++++++++++++++++++++
sql structure :-
mysql> describe fashion;
+--------------+--------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| GENDER | varchar(6) | YES | | NULL | |
| links | varchar(255) | YES | | NULL | |
| category | varchar(255) | YES | | NULL | |
| item_content | varchar(255) | YES | | NULL | |
| price | varchar(10) | YES | | NULL | |
| seller | varchar(20) | YES | | NULL | |
| created_on | timestamp | NO | | CURRENT_TIMESTAMP | |
+--------------+--------------+------+-----+-------------------+----------------+
8 rows in set (0.00 sec)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
error:
query = query % db.literal(args)
TypeError: not all arguments converted during string formatting
[toc] | [next] | [standalone]
| From | Ervin Hegedüs <airween@gmail.com> |
|---|---|
| Date | 2013-12-13 14:26 +0100 |
| Message-ID | <mailman.4069.1386941301.18130.python-list@python.org> |
| In reply to | #61820 |
hello,
On Fri, Dec 13, 2013 at 05:18:59AM -0800, Jai wrote:
> sql = """insert into
> fashion(GENDER,links,category,item_content,price,seller) \
> VAlUES('%s','%s','%s','%s','%s','s')"""
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
may be you miss a "%" sign?
> query = query % db.literal(args)
> TypeError: not all arguments converted during string formatting
cheers:
a.
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-12-13 14:33 +0100 |
| Message-ID | <mailman.4070.1386941628.18130.python-list@python.org> |
| In reply to | #61820 |
Jai wrote:
> my code :
> sql = """insert into
> fashion(GENDER,links,category,item_content,price,seller)
> VAlUES('%s','%s','%s','%s','%s','s')"""
> cursor.execute(sql,(gender,main_link,cat,item_content,price,seller))
This looks very much like your previous mistake
(1) Don't put quotes around the placeholders. They are probably interpreted
as string literals.
(2) The last placeholder is missing the %
> error:
>
>
> query = query % db.literal(args)
> TypeError: not all arguments converted during string formatting
Note that we prefer to see complete tracebacks as this usually simplifies
debugging a lot.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web