我想解决几个问题:
1.第1-3列可能是合并的单元格,因此缺少“#td”。值,如果这些列不存在td,我如何填充每一行?或者,列4-7始终可见。我考虑处理这个问题的一种方法是向后循环以获得第7,6,5,4列,如果第3列,第2列不存在则使用前一行的先前值
2.在第4列中,可能有多个值和一个或多个超链接。我需要提取文本和所有超链接以单击它们并下载附加的文件。
如果有比硒更好的方法,请告诉我。最终输出是将此数据表和附件填充到Excel文件中。
Python - Selenium代码:此代码用于通过HTML代码中的每一列提取每一行数据(' td')
table = driver.find_element_by_id('table')
row = table.find_elements_by_tag_name("tr")
for rows in row:
cols = rows.find_element_by_tag_name("td")
for col in cols:
print(col.text)
我附上了使用Beautiful Soup提取的HTML代码,让您了解该表的外观。第一个表格行有7' td'但后续行没有。
确定。所以我对col代码做了以下改进。扭转循环工作完美!现在我的所有超链接将始终位于第4列。现在我必须找出列1-3以填充每一行并打开所有超链接并将它们保存到共享驱动器上的特定文件夹。谢谢!
columncounter = 7
cols = rows.find_elements_by_tag_name("td")
for col in reversed(cols):
print('ColumnNumber = %d' %columncounter)
print(col.text)
if columncounter == 4:
colfour = col.get_attribute('innerHTML')
colfour2 = col.find_elements_by_tag_name('a')
for a in colfour2:
print(a.get_attribute('href'))
columncounter-=1
答案 0 :(得分:0)
这可能不是最好的解决方案,但是我这样做是为了让它发挥作用:
from selenium import webdriver
from win32com.client as win32
xl = win32.gencache.EnsureDispatch('Excel.Application')
xl.Visible = 1
wb = xl.Workbooks.Open('Template.xlsx')
xl.DisplayAlerts = False
ws = wb.Worksheets('Sheet1')
tr = 10 # paste results into row 10 for excel table
for rows in row[1:] #skip header row
rtxt = rows.text.strip() #trim text to determine if row is empty
if rtxt: # used to determine if rtxt variable for empty row
columncounter = 7
cols = rows.find_elements_by_tag_name("td")
for col in reversed(cols):
if columncounter == 7:
col7 = col.text
elif columncounter == 6:
col6 = col.text
elif columncounter == 5:
col5 = col.text
elif columncounter ==4:
col4 = col.text
colfour = col.get_attribute('innerHTML') #get entire cell code
colfour2 = col.find_elements_by_tag_name('a')
for a in colfour2:
link = a.get_attribute('href') #extract hyperlink
linkh = 'https'
if linkh in link: #only want hyperlinks that start with https
for i in link:
col8+=i #extracts multiple link into same variable
elif columncounter == 3:
col3 = col.text
elif columncounter == 2:
col2 = col.text
elif columncounter == 1:
col1 = col.text
columncounter-=1
#paste all of the values in column into excel
dest_cell = ws.Range('A' + str(tr))
dest_cell.Value = col1
dest_cell = ws.Range('B' + str(tr))
dest_cell.Value = col2
dest_cell = ws.Range('C' + str(tr))
dest_cell.Value = col3
dest_cell = ws.Range('D' + str(tr))
dest_cell.Value = col4
dest_cell = ws.Range('E' + str(tr))
dest_cell.Value = col5
dest_cell = ws.Range('F' + str(tr))
dest_cell.Value = col6
dest_cell = ws.Range('G' + str(tr))
dest_cell.Value = col7
dest_cell = ws.Range('H' + str(tr))
dest_cell.Value = col8
tr+=1
else:
continue #skip empty rows
这将附加第1-3列的值,因为没有' td'由于前3列中的合并单元格,每行的所有7列的值。然后将每个值粘贴到excel文件中作为重复值,直到它根据columncounter获取从该列的col.text检索到的新值。