gpt4 book ai didi

Perl WWW::Mechanize Parse Content 问题?

转载 作者:行者123 更新时间:2023-12-04 18:14:37 25 4
gpt4 key购买 nike

我正在使用 Perl 的 WWW::Mechanize 库从网站上抓取内容。但是,我注意到网页的原始 HTML 源代码和 WWW::Mechanize 解析的内容不同。因此,我的脚本中的一些功能被破坏了。

所以,这是脚本(一个子集,只是为了演示错误/问题)

#! /usr/bin/perl

use WWW::Mechanize;
use warnings;

$mech=WWW::Mechanize->new();
$mech->stack_depth(0);

$url="http://www.example.com";

$mech->get($url);

print $mech->content;

简短的代码,它将连接到网站并检索整个 HTML 页面。

我运行脚本并将输出重定向到一个文本文件,以便我可以分析它们。
perl test.pl >> source_code.txt

现在,当我比较 source_code.txt 和浏览器(Firefox)显示的网站的实际源代码时,存在差异。

例如:
<tr>
<td nowrap="nowrap">This is Some Text</td>
<td align="right"><a href="http://example.com?value=key">Some more Text</a></td>
</tr><tr>

上面的源代码是在浏览器中观察到的。 (查看页面源功能)

但是,在文本文件中,source_code.txt(由 WWW::Mechanize 生成)

表明:
<tr>
<td nowrap="nowrap">This is some text</td>
<td align="right">This is some more text</td>
</tr><tr>

如您所见,嵌套在第二组标签之间的 anchor 标签被删除了。

这是一个已知问题还是我需要使用 $mech->content 以外的其他东西来查看源代码?

谢谢。

最佳答案

这是一种常见的行为,称为 "user agent sniffing"。 ,例如对于盲人用户,页面将显示不同。您可以使用不同的插件在浏览器中更改您的用户代理字符串,也可以使用@LHMathies。说,在 WWW::Mechanize 见 UserAgent.pmMechanize->new

例子 :

my $mech = WWW::Mechanize->new( agent => 
'Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)'
);

另见 a list of common user agent strings .

关于Perl WWW::Mechanize Parse Content 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11964887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com