从数据库取出所有的URL
url = """select link from table_name"""
cur.execute(url)
url_all = cur.fetchall() # 找到所有的URL
print(type(url_all)) # tuple
print(url_all)
输出结果:
(('http://tieba.baidu.com/p/6159240910?pid=126042701819&cid=0#126042701819',),
('http://tieba.baidu.com/p/6156784099?pid=126006405153&cid=0#126006405153',),
('http://tieba.baidu.com/p/5292362527?pid=123454517619&cid=125846391756#125846391756',),
('http://tieba.baidu.com/p/6104892971?pid=125176041436&cid=0#125176041436',))
当把所有的URL链接从数据库取出的时候,URL为元组,将元组转存到列表中
一行代码即可解决:
url_list = [i[0] for i in url_all]
以下为详细过程代码【不推荐,过程过于冗杂,体现不出你的高级】:
循环元组,取元组中的各个值:
for url_a in url_all:
# print(url_a) # 各个值仍为元组形式
url = url_a[0] # 字符串str
print(url)
输出结果:
http://tieba.baidu.com/p/6159240910?pid=126042701819&cid=0#126042701819
http://tieba.baidu.com/p/6156784099?pid=126006405153&cid=0#126006405153
http://tieba.baidu.com/p/5292362527?pid=123454517619&cid=125846391756#125846391756
http://tieba.baidu.com/p/6104892971?pid=125176041436&cid=0#125176041436
将URL数据存入列表:
url_list = [] # 定义一个列表
url_list.append(url)