【问题标题】:Mule ESB : Read HTML骡 ESB : 阅读 HTML
【发布时间】:2015-09-27 06:48:54
【问题描述】:

我有一种情况,我必须解析网页的结果。在这种情况下,网站不提供使用 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>

【问题讨论】:

    标签: mule


    【解决方案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>

    【讨论】:

    • 主机可以是host="resellerratings.com"www可以删除..我想这也可以
    【解决方案2】:

    试试这个:

    <?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 页面:

    现在,为了从这个网络获取信息。我认为骡子不是一个选择。您需要一个允许您操作 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 组件并使用 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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-29
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      • 1970-01-01
      • 1970-01-01
      • 2014-02-02
      相关资源
      最近更新 更多