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/