日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python读取excel送到网页_python+selenium excel中文读取填充到网页

發布時間:2025/3/12 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python读取excel送到网页_python+selenium excel中文读取填充到网页 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

由于我技術水平,磨磨唧唧很長時間終于弄出來了,在整理的時候思考了下,可能是我當時太關注打印出來的list了 ,list里不能顯示中文,我就初以為我總是錯了。

附上表格一部分

#coding:utf-8

importxlrddefopen_excel(file):try:

data=xlrd.open_workbook(file)returndataexceptException as e:print(str(e))def excel_table_byindex(file,colnameindex = 0,by_index=0):

data=open_excel(file)

table=data.sheets()[by_index]

nrows=table.nrows#行

colnames =table.row_values(colnameindex)#列內容

list=[]for rownum in range(1, nrows):

row=table.row_values(rownum)ifrow:

app={}for i inrange(len(colnames)):

s=row[i]#if isinstance(s, unicode):

#s = row[i].encode("utf-8")

print "每一個", s

app[colnames[i]]=s

list.append(app)printrowprintlistreturnlist

listdata=excel_table_byindex("E:\py\\alwaystry\sign_up.xlsx",0)

在開頭加了#coding:utf-8,就已經是utf-8格式的了,所以能處理中文。

打印print"每一個", s 的時候,打印出來是 每一個 二毛a

printrow 打印出來是[u'\u4e8c\u6bdba',...

printlist 打印出來就是[{u'nickname': u'\u4e8c\u6bdba'..., 所有的行數據。

當初的我只看到了Unicode編碼就以為他是打印不了中文了,其實是list不能顯示。現在想來,還是太嫩

在漫長的探索中,我知道了str和Unicode

(我看著覺得不錯的網址http://wklken.me/posts/2013/08/31/python-extra-coding-intro.html)

反正就是

str -> decode('str的編碼格式') -> unicode

unicode -> encode('你要的格式') -> str

要查看編碼格式,就print chardet.detect(listdata[i]['nickname']),判斷字符編碼格式當然還是要import chardet的

他有時候會報ValueError: Expected a bytes object, not a unicode object錯,那也就知道他是什么編碼格式了。

還是這樣吧,直接打印格式:print type(listdata[i]['realname'])打印輸出,說明人家已經是unicode,那你就把chardet.detect()刪掉就好。

UTF-8是Unicode的實現方式之一/utf8是對unicode字符集進行編碼的一種編碼方式。(當時做筆記記下來的,我覺得這兩句話還是蠻好理解的)

如果,很不慎,你走到了和我當初一樣的境地,早早的把encode,就是注釋掉的那兩句。

提示UnicodeDecodeError: 'utf8' codec can't decode byte 0xe4 in position 0: unexpected end of data等等,不妨嘗試一下decode。如果是下拉框匹配的情況,可以先decode再encode試試,下面代碼最后一行。

整體大概代碼如下

#輸入網址

address = raw_input('Enter location:')if len(address) < 1:print "error"url=address#放你的配置文件

profile_dir = r"C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\5cmfbcqp.default"profile=webdriver.FirefoxProfile(profile_dir)

driver=webdriver.Firefox(profile)#打開excel

defopen_excel():try:

data=xlrd.open_workbook(file)returndataexceptException as e:print(str(e))def excel_table_byindex(file,colnameindex = 0,by_index=0):

data=open_excel(file)

table=data.sheets()[by_index]

nrows=table.nrows#行

colnames =table.row_values(colnameindex)#列內容

list=[]for rownum in range(1, nrows):

row=table.row_values(rownum)ifrow:

app={}for i inrange(len(colnames)):

s =row[i]ifisinstance(s, unicode):

s= row[i].encode("utf-8")prints

app[colnames[i]] =s

list.append(app)print(list)returnlist#通過css判斷是否存在,你也可以用if else

defjudgewithcss(css):try:

driver.find_element_by_css_selector(css)except:returnFalsereturnTrue

listdata=excel_table_byindex("E:\sign_up.xlsx",0)if(len(listdata)<0):assert 0,u"數據異常"

for i inrange(0, len(listdata)):

driver.get(url)#昵稱,這是個input

if judgewithcss("#nickname") ==True:

nickname= driver.find_element_by_id("nickname")

nickname.clear()print chardet.detect(listdata[i]['nickname'])

nickname.send_keys(listdata[i]['nickname'].decode("utf-8"))#血型,這是個select

if judgewithcss("#blood") ==True:

blood= driver.find_element_by_id("blood")#以下3種也能判斷出格式

#print isinstance(listdata[i]['blood'], unicode)

#print isinstance(listdata[i]['blood'], str)

#print type(listdata[i]['blood'])

print chardet.detect(listdata[i]['blood'])

Select(blood).select_by_value(listdata[i]['blood'].decode("windows-1252").encode("windows-1252"))

總結

以上是生活随笔為你收集整理的python读取excel送到网页_python+selenium excel中文读取填充到网页的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。