屏幕抓取:自动化 vim 脚本
在vim中,我将一系列网页(一次一个)加载到vim缓冲区中(使用vim netrw插件),然后解析html(使用vim elinks插件)。一切都好。然后,我使用正则表达式编写了一系列 vim 脚本,最终结果为几千行,其中每行的格式正确 (csv),以便上传到数据库。
为了做到这一点,我必须使用 vim 的标记功能,以便我可以循环遍历文档的特定点并将其重新组装成一个 csv 行。现在,我正在考虑使用 Perl 的“Mechanize”类库(UserAgent 等)来自动化此操作。
问题:
- vim 能否“标记”文档的各个部分(以便 执行替换)是在 Perl 中完成的吗?
- 建议直接使用“elinks” - 我认为这意味着 使用 ellinks 将页面加载到无头浏览器中并执行 Perl 那里的内容上的脚本(?)
- 如果这是正确的,是否会出现部署问题 当我将站点从本地主机 LAMP 堆栈设置迁移到 像 Bluehost 这样的托管公司?
谢谢
编辑1:
尝试将知识从VIM迁移到PERL:
如果@flesk(下面)是正确的,那么我将如何执行这个在a中“标记”行的例程(用vim编写)文本文件(“i”和“j”),然后使用它作为范围('i,'j)来执行最后两次替换?
:g/^\s*\h/d|let@"=substitute(@"[:-2],'\s\+and\s\+',',','')|ki|/\n\s*\h\|\%$/kj|
\ 'i,'js/^\s*\(\d\+\)\s\+-\s\+The/\=@".','.submatch(1).','/|'i,'js/\s\+//g
我在 perldoc perlre 手册中没有看到此功能。我是否缺少模块或对 m/ 或 qr/ 的一些基本 Perl 理解?
In vim, I loaded a series of web pages (one at a time) into a vim buffer (using the vim netrw plugin) and then parsed the html (using the vim elinks plugin). All good. I then wrote a series of vim scripts using regexes with a final result of a few thousand lines where each line was formatted correctly (csv) for uploading into a database.
In order to do that I had to use vim's marking functionality so that I could loop over specific points of the document and reassemble it back together into one csv line. Now, I am considering automating this by using Perl's "Mechanize" library of classes (UserAgent, etc).
Questions:
- Can vim's ability to "mark" sections of a document (in order to
perform substitutions on) be accomplished in Perl? - It was suggested to use "elinks" directly - which I take to mean to
load the page into a headless browser using ellinks and perform Perl
scripts on the content from there(?) - If that's correct, would there become a deployment problem with
elinks when I migrate the site from my localhost LAMP stack setup to
a hosting company like Bluehost?
Thanks
Edit 1:
TYRING TO MIGRATE KNOWLEDGE FROM VIM TO PERL:
If @flesk (below) is right, then how would I go about performing this routine (written in vim) that "marks" lines in a text file ("i" and "j") and then uses that as a range ('i,'j) to perform the last two substitutions?
:g/^\s*\h/d|let@"=substitute(@"[:-2],'\s\+and\s\+',',','')|ki|/\n\s*\h\|\%$/kj|
\ 'i,'js/^\s*\(\d\+\)\s\+-\s\+The/\=@".','.submatch(1).','/|'i,'js/\s\+//g
I am not seeing this capability in the perldoc perlre manual. Am I missing either a module or some basic Perl understanding of m/ or qr/ ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我确信您所需要的只是某种HTML 解析器。例如,我正在使用 HTML::TreeBuilder::XPath。
I'm sure all you need is some kind of HTML parser. For example I'm using HTML::TreeBuilder::XPath.