python - 如何编写一个可以浏览的Python机器人

标签 python robot

Possible Duplicate:
Where shall I start in making a scraper or a bot using python?

我知道这显然是可能的...... 我被要求实现某种机器人,它可以访问网站、登录、访问一组链接、使用日期输入填写搜索表单以获取 XLS 文件并注销。 如果手动完成,整个过程需要将近一个小时,因此脚本/机器人将为我们节省大量时间。

想法?图书馆?我想我需要 urllib?
或者根本不使用Python?
提前致谢!

编辑:我搜索了很多“python爬虫”,直到评论之前才发现Mechanize或Scrapy:/
我将首先进一步研究机械化。谢谢。

最佳答案

我是 twill 的粉丝 python 模块。这是我不久前用来进行基本浏览和抓取的一小部分代码示例。

import twill
import twill.commands as c

def login():
    c.clear_cookies()
    c.go('http://icfpcontest.org/icfp10/login')
    c.fv(1, 'j_username', 'Side Effects May Include...')
    c.fv(1, 'j_password', '<redacted>')
    c.submit()
    c.save_cookies('/tmp/icfp.cookie')

all_cars_rx = re.compile(r'<td style="width: 20%;">(\d+)</td><td>(\d+)</td>')
def list_cars():
    c.go('http://icfpcontest.org/icfp10/score/instanceTeamCount')
    cars = re.findall(all_cars_rx, c.show())
    if not cars:
        sys.stderr.write(c.show())
        sys.stderr.write('Could not find any cars')
    return cars;

值得一提的是,不应使用正则表达式来解析 HTML。您所看到的是在很短的时间内针对 ICFP 进行的肮脏黑客攻击。

关于python - 如何编写一个可以浏览的Python机器人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5544008/

相关文章:

Python Django 获取用户输入

python - 在应用程序上下文之外工作 - Flask

gravity - 运行模拟后,机器人模型从地板上掉落

Python for Naoqi(动态模块未正确初始化)

indexing - 谷歌索引图像中文本内容的最佳方法?

python - 使用 pip 安装 face_recognition 模块时出错

python - 如何使用 CLSID 值和 C++ 头文件打开带有 ctypes 的库?

python - 如何在 Django 管理中显示 auto_now_add 字段?

c - AVR 工作室 Nibobee 图书馆

Python 进程间总线