1、Python3中使用xpath解析循环中的html页面时,一直在重复第一个值,但是print(tr.xpath('string(.)'))这段代码的值一直在循环,怀疑是下面取值的语法不对
代码如下:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
from lxml import etree
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(executable_path = r'D:chromedriver_win32chromedriver.exe',chrome_options=chrome_options)
url = '/assortm...'
driver.get(url)
time.sleep(5)
html = driver.page_source
selector = etree.HTML(html)
trEles = selector.xpath('//*[@id="tableData_"]/div[2]/table/tbody/tr')
for tr in trEles:
print(tr.xpath('string(.)'))
code = tr.xpath('//td/a/text()')[0]
companybbreviation = tr.xpath('//td[2]/text()')[0]
listingDate = tr.xpath('//td/text()')[3]
generalCapital = tr.xpath('//td/div/text()')[0]
LIQUI = tr.xpath('//td/div/text()')[1]
print(code + ' : ' + companybbreviation + ' : ' + listingDate+' : '+generalCapital+' : '+LIQUI)
print('-----------------------------------')![图片描述][1]
这是打印出来的值,都是重复的
如果觉得《python3 xpath_Python3使用xpath解析》对你有帮助,请点赞、收藏,并留下你的观点哦!