Perl 网络抓取工具,从 DIV 中提取仅具有“样式”的内容标签?
我被困在这个问题上一整天了..我对 Perl 中的解析/抓取还很陌生,但我以为我已经掌握了它直到这..我一直在尝试使用不同的 Perl 模块(tokeparser,tokeparser:简单,网络解析器和其他一些)...我有以下字符串(实际上是整个 HTML 页面,但这只是显示相关部分..我正在尝试提取“text1”和“text1_a”。等等(“text1”等只是作为示例放在那里)...所以基本上我认为我需要先从每个中提取这个:
"<span style="float: left;">test1</span>test1_a"
然后解析它以获得两个值..我不'不知道为什么这给我带来了这么多麻烦,因为我以为我可以在 tokeparser:simple 中完成它,但我似乎无法返回 DIV 内部的值,我想知道它是否因为它包含另一组标签(标签)
字符串(代表html网页)
<div id="dataID" style="font-size: 8.5pt; width: 250px; color: rgb(0, 51, 102); margin-right: 10px; float: right;">
<div style="width: 250px; text-align: right;"><span style="float: left;">test1</span>test1_a</div>
<div style="width: 250px; text-align: right;"><span style="float: left;">test2</span>test2_a</div>
<div style="width: 250px; text-align: right;"><span style="float: left;">test3</span>test3_a</div>
我在perl web解析器模块中的尝试:
my $uri = URI->new($theurl);
my $proxyscraper = scraper {
process 'div[style=~"width: 250px; text-align: right;"]',
'proxiesextracted[]' => scraper {
process '.style', style => 'TEXT';
};
result 'proxiesextracted';
我只是盲目地试图理解web:parser模块,因为基本上没有关于它的文档,所以我只是从他们在模块中包含的示例以及我在互联网上找到的示例..非常感谢任何建议。
I'm stuck on this and have been all day.. I'm still pretty new to parsing / scraping in perl but I thought I had it down until this.. I have been trying this with different perl modules (tokeparser, tokeparser:simple, web parser and some others)... I have the following string (which in reality is actually an entire HTML page, but this is just showing the relevant part.. I am trying to extract "text1" and "text1_a".. and so on (the "text1", etc is just put in there as an example)... so basically I think I need to extract this first from each:
"<span style="float: left;">test1</span>test1_a"
Then to parse this to get the 2 values.. I don't know why this is giving me so much trouble as I thought I could just do it in tokeparser:simple but I couldn't seem to return the value inside of the DIV, I wonder if its because it contains another set of tags (the tags)
string (represents html web page)
<div id="dataID" style="font-size: 8.5pt; width: 250px; color: rgb(0, 51, 102); margin-right: 10px; float: right;">
<div style="width: 250px; text-align: right;"><span style="float: left;">test1</span>test1_a</div>
<div style="width: 250px; text-align: right;"><span style="float: left;">test2</span>test2_a</div>
<div style="width: 250px; text-align: right;"><span style="float: left;">test3</span>test3_a</div>
my attempt in perl web parser module:
my $uri = URI->new($theurl);
my $proxyscraper = scraper {
process 'div[style=~"width: 250px; text-align: right;"]',
'proxiesextracted[]' => scraper {
process '.style', style => 'TEXT';
};
result 'proxiesextracted';
I'm just kind of blindly trying to make sense of the web:parser module as there is essentially no documentation on it so I just pieced that together from the examples they included with the module and one I found on the internet.. any advice is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你想要一个 DOM 解析器(更容易使用树浏览,稍微慢一些)。尝试 HTML::TreeBuilder
HTML::Element 手册页(包含模块)
让我们得出你的答案:
If you want a DOM parser (easier to use tree browsing, slightly slower). Try HTML::TreeBuilder
HTML::Element man page (module is included)
Which leads us to your answer:
使用 Web::Scraper,尝试:
它给出:
希望这有帮助
using Web::Scraper, try :
It give :
Hope this helps