直接上代码
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
chrome_driver=r'D:\python\Lib\site-packages\selenium\webdriver\chrome\chromedriver.exe'
driver=webdriver.Chrome(executable_path=chrome_driver)
waite = WebDriverWait(driver,10)
def search():
driver.get('https://s.weibo.com/top/summary?cate=realtimehot')
divs = driver.find_elements_by_xpath('//*[@id="pl_top_realtimehot"]/table/tbody/tr[8]/td[2]')
for div in divs:
i = 1
while(i<50):
i = i+1
title = div.find_element_by_xpath('//*[@id="pl_top_realtimehot"]/table/tbody/tr[%s]/td[2]/a' % i).text
clicks = div.find_element_by_xpath('//*[@id="pl_top_realtimehot"]/table/tbody/tr[%s]/td[2]/span' % i).text
link = div.find_element_by_xpath('//*[@id="pl_top_realtimehot"]/table/tbody/tr[%s]/td[2]/a' % i).get_attribute("href")
priduct = {"标题":title,"点击数":clicks,"链接":link}
print(priduct)
if __name__=='__main__':
search()
效果图:
使用的是Web应用程序测试的工具–selenium—
from 失败自己pip install 一下就可以了
还需要下载谷歌驱动chromedriver.exe
先打开chrome浏览器,输入chrome://version/ 查看自己的版本号
下载地址:http://chromedriver.storage.googleapis.com/index.html
下载对应版本
代码里这里替换掉就可以了