zoukankan      html  css  js  c++  java
  • Python获取 东方财富 7x24小时全球快讯

    本文使用的IDE为PyCharm。

    1.第三方库

    (1)selenium

    selenium用来做浏览器自动化,因为这部分信息是动态加载的,不用这种方法读取不到相关数据。

    安装:

    pip3 install selenium -i https://pypi.tuna.tsinghua.edu.cn/simple

    然后下载与自己浏览器对应的驱动

    以上是网上有大佬贴出来的下载地址,实际使用需要根据自己电脑上的浏览器版本下载对应的驱动,比如我用的谷歌浏览器,驱动版本不对应会报如下错误:

    image

    这个时候,我们根据这里报的 chrome=75.0.3770.142去查找对应的驱动版本。驱动版本可到 http://npm.taobao.org/mirrors/chromedriver/ 下载。

    我的驱动直接放在文件同目录了:

    image

    (2)BeautifulSoup

     

    2.网页分析

    东方财富 7x24小时全球快讯 的网址是 http://m.eastmoney.com/kuaixun ,这里我想获取三项内容,新闻时间,新闻简介和新闻的链接。

    image

    image

     

    3.代码实现及效果

    代码中关键的地方都已经写在注释里面了。

    from bs4 import BeautifulSoup
    import time;
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    chrome_options = Options()
    # 设置chrome浏览器无界面模式,不然每运行一次都会弹出来谷歌浏览器界面
    # 不过弹出谷歌界面有助于理解为什么下面会有个页面滚动
    chrome_options.add_argument('--headless')
    # executable_path为驱动地址
    browser = webdriver.Chrome(executable_path='./chromedriver.exe', chrome_options=chrome_options)
    url = "http://m.eastmoney.com/kuaixun"
    browser.get(url)
    # 模仿浏览器往下滚动的页面,获取更多的数据
    for i in range(1, 5):
        browser.execute_script('window.scrollTo(0,document.body.scrollHeight)')
        time.sleep(1)
    html = BeautifulSoup(browser.page_source, "lxml")
    # 退出浏览器
    browser.quit()
    # print(html)
    news_list = html.find_all('div', class_='kxitem')
    # print(news_list)
    for news in news_list:
        print(news['data-id'])
        news_text = news.find('span')
        news_href = news.find('a')
        for s in news_text("a"):
            # 去掉span标签中的链接标签
            s.extract()
        print(news_text.get_text())
        print(news_href['href'])

    效果:

    image





  • 相关阅读:
    Date类型 方法
    迭代方法和归并函数
    js快速排序方法
    reset
    水平垂直居中
    css清除浮动
    box-shadow
    display---我的第一篇博客
    centos7基础安装
    aws和ufile挂载数据盘EBS
  • 原文地址:https://www.cnblogs.com/betterwgo/p/11255581.html
Copyright © 2011-2022 走看看