[英]Perl+Selenium: chomp() fails
I'm using Selenium for work and I have extract some data from "//ul", unfortunately this data contains a newline, I tried to use chomp() function to remove this (because I need to write in a CSV's file) but it's not working, the portion of code is:我正在使用 Selenium 工作,我从“//ul”中提取了一些数据,不幸的是,这些数据包含换行符,我尝试使用 chomp() function 来删除它(因为我需要写入 CSV 文件)但是它不起作用,代码部分是:
open (INFO, '>>file.csv') or die "$!";
print INFO ("codice\;descrizione\;prezzo\;URLFoto\n");
my $sel = Test::WWW::Selenium->new( host => "localhost",
port => 4444,
browser => "*chrome",
browser_url => "http://www.example.com/page.htm" );
$sel->open_ok("/page.htm");
$sel->click_ok("//table[2]/tbody/tr/td/a/img");
$sel->wait_for_page_to_load_ok("30000");
my $descrizione = $sel->get_text("//ul");
my $prezzo = $sel->get_text("//p/font");
my $codice = $sel->get_text("//p/font/b");
my $img = $sel->get_attribute ("//p/img/\@src");
chomp ($descrizione);
print INFO ("$codice\;$descrizione\;$prezzo\;$img\n");
$sel->go_back_ok();
# Close file
close (INFO);
but the output is:但是 output 是:
Art. S500 Set Yoga "Siddhartha";Idea regalo ?SET YOGA Siddhartha? Elegante scatola in cartone lucido contenente:
2 mattoni in legno naturale mis. cm 20 x 12,5 x 7
1 cinghia in cotone mis. cm 4 x 235
1 stuoia in cotone mis. cm 70 x 170
1 manuale di introduzione allo yoga stampato
Tutto rigorosamente realizzato con materiali natural;€ 82,50;../images/S500%20(Custom).jpg
chomp
removes the platform specific end-of-line character sequence from the end of a string or a set of strings. chomp
从一个字符串或一组字符串的末尾删除平台特定的行尾字符序列。
In your case, you seem to have a single string with embedded newlines and/or carriage returns.在您的情况下,您似乎有一个带有嵌入式换行符和/或回车符的字符串。 Hence, you probably want to replace any sequence of possible line endings with something else, let's say a single space character.
因此,您可能希望用其他内容替换任何可能的行结尾序列,比方说单个空格字符。 In that case, you'd do:
在那种情况下,你会这样做:
$descrizione =~ s/[\r\n]+/ /g;
If you want to replace all vertical whitespace, Perl has a special character class shortcut for that:如果要替换所有垂直空格,Perl 有一个特殊字符 class 快捷方式:
use v5.10;
$descrizione =~ s/\v+/ /g;
Use this to remove \r
as well.也可以使用它来删除
\r
。
$descrizione =~ s#[\r\n]+\z##;
regards,问候,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.