我想用python抓取网页里的图片,地址,商铺名,电话号码,怎么写代码

2022-08-15 社会 72阅读
class DB(object):
def __init__(self, host, port, user,
passwd, db, use_unicode=True, charset='utf8'):
self.host = host
self.port = port
self.user = user
self.passwd = passwd
self.db = db
self.use_unicode = use_unicode
self.charset = charset

@property
def conn(self):
if not hasattr(self, '__conn'):
self.__conn = MySQLdb.connect(
host=self.host,
port=self.port,
user=self.user,
passwd=self.passwd,
db=self.db,
use_unicode=self.use_unicode,
charset=self.charset
)
return self.__conn

def reboot_conn(self):
if hasattr(self, '__conn'):
try:
self.__conn.close()
del self.__conn
except:
pass

@catch_2006
def query(self, sql, args=None):
print sql
cursor = self.conn.cursor()
cursor.execute(sql, args)
cursor.execute('commit')
cursor.close()

@catch_2006
def select(self, sql, args=None, is_dict=False, is_one=False):
# print sql, args
if is_dict:
cursor = self.conn.cursor(cursorclass=DictCursor)
else:
cursor = self.conn.cursor()
cursor.execute(sql, args)
if is_one:
resultset = cursor.fetchone()[0]
else:
resultset = cursor.fetchall()
cursor.close()
return resultset
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com