我已经找到了许多参考,滚动整个网页,但我寻找一个特定的部分滚动。我正在研究市场观察网-部分-最新新闻选项卡。如何使用SeleniumWebDriver滚动最新的新闻选项卡?你知道吗
下面是我的代码,它返回新闻标题,但不断重复相同的标题。你知道吗
from bs4 import BeautifulSoup
import urllib
import csv
import time
from selenium import webdriver
count = 0
browser = webdriver.Chrome()
browser.get("https://www.marketwatch.com/newsviewer")
pageSource = browser.page_source
soup = BeautifulSoup(pageSource, 'lxml')
arkodiv = soup.find("ol", class_="viewport")
while browser.find_element_by_tag_name('ol'):
browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(0.5)
div = list(arkodiv.find_all('div', class_= "nv-details"))
heading = []
Data_11 = list(soup.find_all("div", class_ = "nv-text-cont"))
datetime = list(arkodiv.find_all("li", timestamp = True))
for sa in datetime:
sh = sa.find("div", class_ = "nv-text-cont")
if sh.find("a", class_ = True):
di = sh.text.strip()
di = di.encode('ascii', 'ignore').decode('ascii')
else:
continue
print di
heading.append((di))
count = count+1
if 'End of Results' in arkodiv:
print 'end'
break
else:
continue
print count
这是因为您正在执行的脚本会滚动到页面底部。你知道吗
要在获取新闻的元素中保持滚动,需要替换以下内容:
有了这个:
编辑
这是完整的工作解决方案:
编辑2
您可能还需要更改头的存储方式,因为您当前的方式会在列表中保留重复项。将其更改为
set
,这样就不会发生这种情况。你知道吗相关问题 更多 >
编程相关推荐