Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
来自for循环的Python Selenium文本提取列表输出_Python_Selenium_For Loop_Web Scraping - Fatal编程技术网

来自for循环的Python Selenium文本提取列表输出

来自for循环的Python Selenium文本提取列表输出,python,selenium,for-loop,web-scraping,Python,Selenium,For Loop,Web Scraping,我正在使用Python/Selenium从网站中提取一些文本,以便在Google表单中对其进行进一步排序 有15个标题,我需要提取文本。该文本位于标记h5的每个标题下 下面是标题的一个摘录: <tr class="dayHeader"><td colspan="7" style="padding:10px 0;"><hr><h5>&nbsp;&nbsp;Thursday -

我正在使用Python/Selenium从网站中提取一些文本,以便在Google表单中对其进行进一步排序

有15个标题,我需要提取文本。该文本位于标记h5的每个标题下

下面是标题的一个摘录:

<tr class="dayHeader"><td colspan="7" style="padding:10px 0;"><hr><h5>&nbsp;&nbsp;Thursday - 28 January 2021</h5></td></tr>
  <td colspan="7" style="padding:10px 0;"><hr><h5>&nbsp;&nbsp;Thursday - 28 January 2021</h5></td>
    <hr>
    <h5>&nbsp;&nbsp;Thursday - 28 January 2021</h5>
    </td>
  </tr>
上面的for循环输出以下列表:

['Result 1']
['Result 1', 'Result 2']
['Result 1', 'Result 2', 'Result 3']
相反,我如何将其输出:

['Result 1', 'Result 2', 'Result 3']

打印的缩进错误
将其从循环中删除,如:

headers = driver.find_elements_by_tag_name('h5')
results = []

for header in headers:
    result = header.text
    results.append(result)

print(results)
将只打印以下内容:

['Result 1', 'Result 2', 'Result 3']
而不是:

headers = driver.find_elements_by_tag_name('h5')
results = []

for header in headers:
    result = header.text
    results.append(result)
    print(results)
是否每次迭代都会打印:

['Result 1']
['Result 1', 'Result 2']
['Result 1', 'Result 2', 'Result 3']

上面的for循环不应该输出任何内容。。。复制代码时是否遗漏了打印语句?当然!我怎么可能看不到这一点。谢谢你的帮助!
['Result 1']
['Result 1', 'Result 2']
['Result 1', 'Result 2', 'Result 3']