【问题标题】:Parsing WP Web Scraping Data with Regex使用正则表达式解析 WP Web 抓取数据
【发布时间】:2016-03-14 11:46:54
【问题描述】:

我正在使用 Wordpress 插件“WP Web Scraper”来轻松地从网站收集一些数据。函数 wpws_get_content 返回结果“Raised(number%)”我想把它清理成数字。我设法用以下代码返回(90%)

<p id="number1"><?php echo wpws_get_content('http://my.sportrelief.com/sponsor/sachazarb', '#totalizer_percent', array( 'replace_query' => '/Raised/', 'replace_query_type' => 'regex', 'replace_with' => ' ', ) ); ?></p>

我无法进一步解决。奇怪的是,它还返回重复数据,即

<!--
 Start of web scrap (created by wp-web-scraper)
 Source URL: http://my.sportrelief.com/sponsor/sachazarb
 Query: #totalizer_percent (cssselector)
 Other options: Array
(
    [headers] => 
    [cache] => 60
    [useragent] => WPWS bot (http://windreeladprint.com)
    [timeout] => 2
    [on_error] => error_show
    [output] => html
    [glue] => 

    [eq] => 
    [gt] => 
    [lt] => 
    [query_type] => cssselector
    [remove_query] => 
    [remove_query_type] => cssselector
    [replace_query] => /Raised/
    [replace_query_type] => regex
    [replace_with] =>  
    [basehref] => 1
    [a_target] => 
    [callback_raw] => 
    [callback] => 
    [debug] => 1
    [charset] => UTF-8
)
--><span id="totalizer_percent" class="percent">  (90%)</span><span id="totalizer_percent" class="percent">  (90%)</span><!--
 End of web scrap
 WPWS Cache Control: Remote-fetch via WP_Http
 Computing time: 1.306 seconds
-->

【问题讨论】:

    标签: php regex wordpress web-scraping


    【解决方案1】:

    您可以尝试使用此正则表达式 Raised\((\d+)%\) 并使用 \1$1 替换为第一个捕获的组。

    Regex101 Demo

    【讨论】:

    • 感谢 noob 可以删除多余的内容,捕获的组仍然返回重复的条目,但我认为这可能是插件的问题,而不是 php 错误。干杯。
    • 你能解释一下重复输入是什么意思吗?
    • 它返回了 ID 的多个结果,但是在仔细检查源代码后,我发现他们在页面上重复了他们的代码并将其隐藏,但不显示任何内容。所以实际上源中有两个#totalizer_percent 元素。
    • 好的。我希望这个解决方案对您有所帮助。如果您想在解决方案中添加任何内容,请成为我的客人。
    猜你喜欢
    • 2011-08-10
    • 2021-07-27
    • 2013-11-01
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    • 1970-01-01
    相关资源
    最近更新 更多