Mule ESB:阅读HTML

时间:2015-09-25 22:19:17

标签: mule

我有一种情况需要解析网页的结果。在这种情况下,网站不提供用于检索此数据的API。我创建了一个调用网站的流程,但声明:

消息:发送HTTP请求时出错。消息有效负载的类型为:NullPayload
任何帮助将不胜感激。

<http:request-config name="HTTP_Request_Configuration"   host="http://www.resellerratings.com/" port="80" doc:name="HTTP Request Configuration" basePath="/"/>
<flow name="testFlow">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/testReseller" allowedMethods="GET" doc:name="HTTP"/>
    <http:request config-ref="HTTP_Request_Configuration" path="/store/best_buy" method="GET" doc:name="HTTP" sendBodyMode="NEVER"/>
    <logger message="#[message]" level="INFO" doc:name="Logger"/>
</flow>

2 个答案:

答案 0 :(得分:1)

鉴于您的配置,它可能因主机属性而失败,因为它不应包含协议。试试这个:

<http:request-config name="HTTP_Request_Configuration" host="www.resellerratings.com" port="80" doc:name="HTTP Request Configuration" /> <flow name="testFlow"> <http:listener config-ref="HTTP_Listener_Configuration" path="/testReseller" allowedMethods="GET" doc:name="HTTP"/> <http:request config-ref="HTTP_Request_Configuration" path="/store/best_buy" method="GET" doc:name="HTTP" sendBodyMode="NEVER"/> <logger message="#[message]" level="INFO" doc:name="Logger"/> </flow>

答案 1 :(得分:0)

试试这个:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.6.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">

    <http:request-config name="remote_HTTP_Request_Configuration"   host="www.resellerratings.com" port="80" doc:name="REMOTE HTTP Request Configuration" />
    <http:listener-config name="local_HTTP_Request_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>

    <flow name="testFlow1">
        <http:listener config-ref="local_HTTP_Request_Configuration" path="/testReseller" allowedMethods="GET" doc:name="HTTP"/>
        <http:request config-ref="remote_HTTP_Request_Configuration" path="/store/best_buy" method="GET" doc:name="HTTP" sendBodyMode="NEVER"/>
        <object-to-string-transformer doc:name="Object to String"/>
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
    </flow>

</mule>

转到:http://localhost:8081/testReseller

你得到了html页面:

enter image description here

现在,为了从这个网站获取信息。我认为骡子不是一种选择。你需要一个允许你操作html dom的工具。

这与质量保证/测试自动化有关。当然,我们的java有很棒的工具:

我与你分享我的代码:

  • Jsoup示例:从youtube频道获取视频和图像

https://github.com/jrichardsz/api-java-rest-service-youtube/blob/master/code/src/test/java/org/jrichardsz/youtubeapi/rest/test/TestJSoup.java

在此示例中,我从youtube频道获取所有视频div(特定类),并获得内容和标签。

  • HTMLUnit示例:自动化gogole翻译器:

https://github.com/jrichardsz/appdesktop-super-translator/blob/master/code/src/main/java/com/rnasystems/projects/translator/core/impl/HtmlUnitGoogleUITranslator.java

在这个例子中,我转到谷歌网页翻译,在左框中输入一些单词,按下翻译按钮并从右框中获取响应。全部用java。

最后,你可以使用一些这样的工具作为java componente并使用mule来调用它:

<flow name="testFlowHtmlParser">
    <http:listener config-ref="local_HTTP_Request_Configuration" path="/testReseller" allowedMethods="GET" doc:name="HTTP"/>
    <component doc:name="Java" class="com.mycompany.HtmlParserComponent"/>
</flow>

如果您需要有关html解析器的帮助,请与我联系:

http://jrichardsz.weebly.com/

相关问题