'''百度搜索从数据库里取值'''
import
import
from import
#def sql2():
'E:\\sqlite-tools-win32-x86-3310100\\sqlite-tools-win32-x86-3310100\\test123.db'
'select * from student'
print'results',
#print ("name sex")
for ,in
#字典形式拿数据库里的数据记录
'name',
'sex'
list#把字典key值转换成列表,便于读取
print'打印L',
# 关闭数据库连接
'https://www.baidu.com/'
executable_path"E:/Python3.8/project/chromedriver.exe"
for in
driver.implicitly_wait(8
'kw'#下次输入查询内容之前,先清空上次的内容
'kw'
'su'
10
#driver.close()
#file.close()
#driver.close()#close方法一般关闭一个tab,quit方法才是我们认为的完全关闭浏览器
优化后:(上面循环取值有问题,只能取一次)
'''百度搜索从数据库里取值'''
import sqlite3
import time
from selenium import webdriver
#def openbaidu():
url = 'https://www.baidu.com/'
driver = webdriver.Chrome(executable_path="E:/Python3.8/project/chromedriver.exe")
driver.get(url)
#def sql2():
conn=sqlite3.connect('E:\\sqlite-tools-win32-x86-3310100\\sqlite-tools-win32-x86-3310100\\test123.db')
c=conn.cursor()
c.execute('select * from student')
results = c.fetchall()
print('results',results)
list2=[]
for name,sex in results:
data={#字典形式拿数据库里的数据记录
'name':name ,
'sex':sex
}
L=list(data.values())#把字典key值转换成列表,便于读取
print('打印L',L)
for h in L:
driver.implicitly_wait(8)
driver.find_element_by_id('kw').clear()
driver.find_element_by_id('kw').send_keys(h)
driver.find_element_by_id('su').click()
time.sleep(10)
# driver.close()
# 关闭数据库连接
c.close()
再次优化后:放到函数里
'''打开百度,输入数据库中的数据'''
import sqlite3
import time
from selenium import webdriver
def sql2():
conn=sqlite3.connect('E:\\sqlite-tools-win32-x86-3310100\\sqlite-tools-win32-x86-3310100\\test123.db')
c=conn.cursor()
c.execute('select * from student')
results = c.fetchall()
print('results',results)
return results#返回值 给下面函数调用
#print ("name sex")
c.close()
def getdata() :
'''打开百度,输入数据库中的数据'''
url = 'https://www.baidu.com/'
driver=webdriver.Chrome(executable_path="E:/Python3.8/project/chromedriver.exe")
driver.get(url)
list2=[]
for name,sex in sql2():#直接调用上面函数里的results,等于for name,sex in results
data={#字典形式拿数据库里的数据记录
'name':name ,
'sex':sex
}
L=list(data.values())#把字典key值转换成列表,便于读取
print('打印L',L)
for h in L:
driver.implicitly_wait(8)
driver.find_element_by_id('kw').clear()
driver.find_element_by_id('kw').send_keys(h)
driver.find_element_by_id('su').click()
time.sleep(10)
driver.close()
if __name__ == '__main__':
sql2()
getdata()