V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
automation2022
V2EX  ›  Python

自动化抓取 stackoverflow 问题列表

  •  
  •   automation2022 · 2022-09-03 21:50:15 +08:00 · 2372 次点击
    这是一个创建于 599 天前的主题,其中的信息可能已经有所发展或是发生改变。

    看论坛有人推荐这个 python 自动化开发工具,觉得有点好玩,尝试一下。

    最近在关注 stackoverflow 上的某一类问题, 所以想用程序帮我做监控,自动打开浏览器,搜索指定关键词,然后把前 30 个问题列表保存或者发给我。 源代码贡献到这个github了,以下是我的开发过程.

    系统环境

    安装 clicknium vscode 扩展和 python module ,参照getting started.

    开发思路

    • 自动打开浏览器,返回 tab 对象
    tab = cc.edge.open("www.stackoverflow.com")
    
    • 输入关键字,发送{ENTER}快捷键进行搜索
    tab.find_element(locator.stackoverflow.text_q).set_text(word)
    tab.find_element(locator.stackoverflow.text_q).send_hotkey('{ENTER}')
    
    • 搜索之前,可能会需要进行人机验证,用如下代码进行判断和点击
    elem = tab.wait_appear(locator.stackoverflow.human_verification_div, wait_timeout=5)
    if elem != None:
        elem.click()
    
    • 点击'Newest',根据时间来排序

    • 利用 clicknium 的获取相似元素,获取每个问题的标题,vote 数量,内容,最后更新时间,以及问题的 url

     while catch_count < 30:
            sleep(1)
            elems_title = tab.find_elements(locator.stackoverflow.a_title)
            elems_vote = tab.find_elements(locator.stackoverflow.span_vote)
            elems_content = tab.find_elements(locator.stackoverflow.div_content)
            elems_time = tab.find_elements(locator.stackoverflow.span_time)
     for i in range(len(elems_title)):
                url = "https://www.stackoverflow.com" + elems_title[i].get_property('href')
                item = {
     'Keyword':word, 
     'Title': elems_title[i].get_text(), 
     'Content': elems_content[i].get_text(),
     'Time': elems_time[i].get_text(),
     'Vote': elems_vote[i].get_text(),
     'Url':url}
     print(item)
                catch_count += 1
     if tab.is_existing(locator.stackoverflow.a_next):
                tab.find_element(locator.stackoverflow.a_next).click()
     else:
     break
    

    以下是问题标题链接的 locator locator1

    点击'Validate'是可以验证能匹配到单页 15 个元素的, 通过find_elements1可以一次性获取到所有的元素列表,然后通过get_text()获取文本,针对链接,还可以通过get_property('href') 来获取属性 href 。

    10 条回复    2022-09-26 14:38:08 +08:00
    masker
        1
    masker  
       2022-09-03 21:52:40 +08:00 via Android   ❤️ 6
    然后用 Google translate 翻译一下,做成内容农场?
    automation2022
        2
    automation2022  
    OP
       2022-09-03 21:55:26 +08:00
    @masker 主要是自己使用, 节省自己时间, 之前还做过一个自动把订阅的专栏文章转成 pdf 推送到 kindle 上来阅读,也有点意思。
    BeautifulSoap
        3
    BeautifulSoap  
       2022-09-03 22:39:45 +08:00 via Android   ❤️ 8
    嗯。。。。。。这项目拿来练手可以,但问题在于对 stackoverflow 来说这么费劲真的没有必要

    因为 stackoverflow 每三个月定期提供全站的数据备份下载。如果要新数据还提供 api 给你来用,根本没有这么大费周章搞爬虫的必要

    https://api.stackexchange.com/docs
    ClericPy
        4
    ClericPy  
       2022-09-03 23:14:45 +08:00
    clicknium

    之前扫了眼 Github 没看到源码... 这个开源么, 不开源不太敢用, 倒是把比较常见的东西都综合到一块了
    wxf666
        5
    wxf666  
       2022-09-03 23:18:41 +08:00   ❤️ 1
    @BeautifulSoap 哇,第一次知道 StackOverflow 居然提供全站数据下载。。好开放啊

    @Nillouise 有现成的大型数据库你可以用了
    automation2022
        6
    automation2022  
    OP
       2022-09-03 23:19:54 +08:00
    @BeautifulSoap 嗯,练练手,评估一下是否好用,将来有其他项目上的需求可以多个选择。
    感谢回复,能用 API 解决的肯定是比页面操作要稳定和快速的, 实际自动化项目上经常会遇到中间卡壳环节,没有 API 可用的情况。
    0o0O0o0O0o
        7
    0o0O0o0O0o  
       2022-09-03 23:24:10 +08:00 via iPhone
    fpure
        8
    fpure  
       2022-09-03 23:45:13 +08:00
    @masker 还要微信扫码关注获取验证码才能看🤣
    Nillouise
        9
    Nillouise  
       2022-09-04 10:48:19 +08:00
    @wxf666 多谢,我可以直接拿来玩玩了
    automation2022
        10
    automation2022  
    OP
       2022-09-26 14:38:08 +08:00
    @ClericPy 邮件问了,官方回复是在 roadmap 里面,包括跨平台,预计会在未来的几个月里面做
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4801 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 03:59 · PVG 11:59 · LAX 20:59 · JFK 23:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.