2015-09-25 151 views
0

我有一种情况,我必须解析网页的结果。在这种情况下,该网站不会提供用于检索此数据的API。我创建了一个调用网站的流程,但声明:Mule ESB:阅读HTML

消息:发送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> 

回答

1

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

<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>

+0

主机可以是'host =“resellerratings.com”'和'www'可以删除..我想这也可以 –

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有极好的工具,因为它:

我与你分享我的代码:

  • J汤例如:从YouTube频道

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

获取视频和图像的丝毫不差在这个例子中我得到的所有视频的div(特定类)的YouTube频道,和我得到的内容和标签。

  • 的HtmlUnit例如:自动gogole译者:

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

在这个例子中,我去谷歌网页翻译,把一些词在左框中,按下翻译按钮,并从响应正确的框。所有与Java。

Finaly,你可以使用一些这方面的工具如Java componente并用骡子的调用它:

<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/