By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,734 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

help me~!!!'tuple' object has no attribute 'compile'

P: n/a
my code
-------------------------------------------------------------------------------------------
# -*- coding: utf8 -*-
#!/usr/bin/python

import urllib
import urllib2
import re
import MySQLdb

conn=MySQLdb.connect
(host="localhost",user="root",passwd="ylj",db="net ", charset="utf8")
cur = conn.cursor()
sql='select
net_site.downline_re,net_site.down_re,net_cha.id,n et_cha.urlid,net_cha.source_url
from net_site,net_cha where net_site.id=net_cha.urlid'
cur.execute(sql)
re=cur.fetchall()
if len(re)!=0:
for i in re:
down_re="%s"%(i[1])
source_url="%s"%(i[4])
data= urllib2.urlopen(source_url).read()
res=re.compile(down, re_DOTALL).findall(data)
print str
--------------------------------------------------------------------------------------------------------
when it's run,the eric give error
-------------
The debugged program raised the exception unhandled AttributeError
"'tuple' object has no attribute 'compile'"
File: /var/www/py/down.py, Line: 27
------------
the line:27 -------res=re.compile(down, re_DOTALL).findall(data)
Nov 12 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Nov 12, 9:07*pm, ylj...@gmail.com wrote:
my code
-------------------------------------------------------------------------------------------
# -*- coding: utf8 -*-
#!/usr/bin/python

import urllib
import urllib2
import re
import MySQLdb

conn=MySQLdb.connect
(host="localhost",user="root",passwd="ylj",db="net ", charset="utf8")
cur = conn.cursor()
sql='select
net_site.downline_re,net_site.down_re,net_cha.id,n et_cha.urlid,net_cha.source_url
from net_site,net_cha where net_site.id=net_cha.urlid'
cur.execute(sql)
re=cur.fetchall()
You have bound the name "re" to the tuple returned by cur.fetchall()
This overrides the binding to the re module.
Use "result" or some other meaningful name instead.
if len(re)!=0:
* * *for i in re:
* * * * *down_re="%s"%(i[1])
* * * * *source_url="%s"%(i[4])
* * * * *data= urllib2.urlopen(source_url).read()
* * * * *res=re.compile(down, re_DOTALL).findall(data)
So re is a tuple, not the re module ... splat!

And another problem: down is not defined; do you mean down_re?
* * * * *print str
Here are your next two problems:
(1) str is not defined
(2) str is a type; use some other meaningful name for whatever it is
that you are trying to print

HTH,
John
Nov 12 '08 #2

P: n/a
On Wed, 12 Nov 2008 02:07:46 -0800 (PST), yl****@gmail.com wrote:
[snip]
>
import urllib
import urllib2
import re
import MySQLdb

conn=MySQLdb.connect
(host="localhost",user="root",passwd="ylj",db="net ", charset="utf8")
cur = conn.cursor()
sql='select
net_site.downline_re,net_site.down_re,net_cha.id,
net_cha.urlid,net_cha.source_url
from net_site,net_cha where net_site.id=net_cha.urlid'
cur.execute(sql)
re=cur.fetchall()
This line will hide the "re" that you imported 10 lines before.
if len(re)!=0:
for i in re:
down_re="%s"%(i[1])
source_url="%s"%(i[4])
data= urllib2.urlopen(source_url).read()
res=re.compile(down, re_DOTALL).findall(data)
print str
[snip]

I'm guessing that the "re" returned by cur.fetchall doesn't
have a "compile" function.

--
To email me, substitute nowhere->spamcop, invalid->net.
Nov 12 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.