硒按标记名搜索选项

我正在尝试从名为Correios的网站获取所有数据,在此网站中,我需要处理一些下拉菜单,这些下拉菜单存在一些问题,例如:它正在返回带有一串空字符串的列表。


chrome_path = r"C:\\Users\\Gustavo\\Desktop\\geckodriver\\chromedriver.exe"

driver = webdriver.Chrome(chrome_path)

lista_x = []

driver.get("http://www2.correios.com.br/sistemas/agencias/")

driver.maximize_window()


dropdownEstados = driver.find_elements_by_xpath("""//*[@id="estadoAgencia"]""")


optEstados = driver.find_elements_by_tag_name("option")


for valores in optEstados:

    print(valores.text.encode())

我从中得到的是:


b''

b'ACRE'

b'ALAGOAS'

b'AMAP\xc3\x81'

b'AMAZONAS'

b'BAHIA'

b'CEAR\xc3\x81'

b'DISTRITO FEDERAL'

b'ESP\xc3\x8dRITO SANTO'

b'GOI\xc3\x81S'

b'MARANH\xc3\x83O'

b'MINAS GERAIS'

b'MATO GROSSO DO SUL'

b'MATO GROSSO'

b'PAR\xc3\x81'

b'PARA\xc3\x8dBA'

b'PERNAMBUCO'

b'PIAU\xc3\x8d'

b'PARAN\xc3\x81'

b'RIO DE JANEIRO'

b'RIO GRANDE DO NORTE'

b'ROND\xc3\x94NIA'

b'RORAIMA'

b'RIO GRANDE DO SUL'

b'SANTA CATARINA'

b'SERGIPE'

b'S\xc3\x83O PAULO'

b'TOCANTINS'

b''

b''

b''

b''

b''

b''

b''

b''

b''

b''

b''

b''

b''

b''

b''

b''

b''

b''

b''

b''

b''

b''

b''

b''

b''

b''

b''


如何删除空的b“”?


largeQ
浏览 141回答 3
3回答

LEATH

要检索从文本中的所有<options>的的下拉与ID为estadoAgencia,因为它是一个<select>标签那将是更容易和有效地使用相关联的方法<select>标签,您可以采用如下方案:代码块:estado_select = Select(driver.find_element_by_id('estadoAgencia'))for opt in estado_select.options:&nbsp; &nbsp; print(opt.get_attribute('innerHTML'))控制台输出:ACREALAGOASAMAPÁAMAZONASBAHIACEARÁDISTRITO FEDERALESPÍRITO SANTOGOIÁSMARANHÃOMINAS GERAISMATO GROSSO DO SULMATO GROSSOPARÁPARAÍBAPERNAMBUCOPIAUÍPARANÁRIO DE JANEIRORIO GRANDE DO NORTERONDÔNIARORAIMARIO GRANDE DO SULSANTA CATARINASERGIPESÃO PAULOTOCANTINS
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python