这是我关于StackOverflow的第一篇文章。我的代码运行正常,除非输出不正确。我在此处同时附加了代码段和输出,以便您可以了解出了什么问题。我试图在每行中打印2个项目,这些项目属于2个不同的列表。第一行显示得很好,但在第二行中,第二个列表中的第二个元素不打印。因为它没有给我任何错误,所以我有点迷失了我在做错什么。任何帮助/建议都很好。
try:
blog = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "div.col-lg-8")) # Easy to find by ID and then manipulate inside it.
)
posts = blog.find_elements_by_tag_name("h2")
dates = blog.find_elements_by_css_selector("p.card-text")
for date in dates:
print(date.text)
print(f"Total {len(posts)} posts found")
for i in range(len(posts)):
print(f"Post number {i+1}: ", posts[i].text, "by", dates[i].text)
finally:
driver.quit()
输出看起来像这样:
Tom | April 3, 2020, 11:51 p.m. (Pacific Time)
Tom | March 30, 2020, 4:22 a.m. (Pacific Time)
Total 2 posts found
Post number 1: Just a thought by Tom | April 3, 2020, 11:51 p.m. (Pacific Time)
Post number 2: My first post by
Process finished with exit code 0
如您所见,在by
一词之后,没有任何内容应该是dates
的元素。