【问题标题】:ajax query with curl使用 curl 的 ajax 查询
【发布时间】:2014-03-19 00:52:57
【问题描述】:

我想从一个网站上抓取一下,以收集有关阿根廷机场的信息。该网站是http://www.anac.gov.ar/anac/web/index.php/2/310/informacion-aeronautica/notam。我试图让 curl 获取页面中通过 ajax 查询访问的数据

<script type="text/javascript">
    var lugares = document.getElementById("locations");
    $(lugares).change(
            function() {
                $('.loading').show();
                $('.ajaxbody').hide();
                $.ajax(
                        {
                            url: "/notam/pib",
                            type: "POST",
                            dataType: "html",
                            data: {indicador: $(lugares).val()}
                        }).done(function(html) {
                    tabla = document.getElementById('pib');
                    $(tabla).find('table').remove();
                    $(tabla).append(html);
                    $('.loading').hide();
                    $('.ajaxbody').fadeIn('slow');
                });
            });
</script>

这就是我使用 curl curl -X POST -H "dataType:html" -d'/notam/p 的方式 ib'-H "indicador=TRE" http://www.anac.gov.ar/anac/web/index.php/2/310/informacio n-aeronautica/notam

但是 curl 想要解析 -H "indicador=TRE",因为它是一个 url,但它是在 POST 中发送的数据。

我看到了使用 wireshark 的页面发出的真实 POST,这就是它所看到的

POST /notam/pib HTTP/1.1

Host: 186.153.175.229

User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:27.0) Gecko/20100101 Firefox/27.0

Accept: text/html, */*; q=0.01

Accept-Language: en-US,en;q=0.5

Accept-Encoding: gzip, deflate

Content-Type: application/x-www-form-urlencoded; charset=UTF-8

X-Requested-With: XMLHttpRequest

Referer: http://186.153.175.229/portal/notam

Content-Length: 13

Cookie: PHPSESSID=k1daatvv9882drooofid7cvvc6

Connection: keep-alive

Pragma: no-cache

Cache-Control: no-cache



indicador=TREHTTP/1.1 200 OK

Date: Tue, 18 Feb 2014 23:30:41 GMT

Server: Apache/2.2.22 (Ubuntu)

X-Powered-By: Fat-Free Framework

Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0

Expires: Thu, 19 Nov 1981 08:52:00 GMT

Pragma: no-cache

Access-Control-Allow-Origin: http://geo.anac.gov.ar

Vary: Accept-Encoding

Content-Encoding: gzip

Content-Length: 1512

Keep-Alive: timeout=5, max=100

Connection: Keep-Alive

Content-Type: text/html; charset=UTF-8



...........Y.r.H...O.gm.j."..1DsE.Dj.<P....PF........>..=......QI61...G........f.W.......7..s.x..I.......Q/w.
....P...Zx.^..\.......9.zq....^.N.p.E.!........3.y.On..[.hL.....Q<...7...%.,|h......S....#.D...w.K.._.!...l..
..Q?...]#....g....x}..a.ir..
.E.Hx.eo;.._...4..j..Iqj|.o...[F...|.....:B......Oe`....!0"...,..M..M.O....k.h2,..t...VLPZ-.V...E....C..!.*.v..E..].....s.....-...&.......j2;...G....q.68...2F..G....m..S.[dp$.
..e..Di.. ....AxK.)...S...y.=..7....>?...yA.t.F.@.l.tD..?.ME5l.Z8..8...@.-.....f[8.f..=<4...t.pR(>n.1.............."..4.W.I.2...1UY.U..M.L.....G... et....].5.E
.q...;q.6..?.{... .i.+@*......X.. ...A..6.%0....Z...2.W..J....Mi...n-E......A.G.PM.........*m.4N.......N....p*..............R.v#..
P....)<.c...\..".e...(.3...P.3..%vK....6N..8......9....DD,U %..d...=2.Z.......f..0...........R......\...h....H/F6....M3..m..&6..
..n.qc1...).3..\.+.r.4b.6j.3h...R&..+.D..1kW@DR,C..i.-.....+..Hu...D...."...y.v.6.8.X..R.J.A.B...h.{...H.......y.P..*.t.$z.!....t>....Sa?.....5.t.t..&.r.K5Zx:aI$/#.....N......4.D.k.....3...?x!8..&....|...v....hY
X..{Q.m...d.X.......R...OO"G..U........!.......3?.
...o......QT.NI..%.k. .^...[.*.'....,>.8..s.O....<.vd...O&..>.n.f..E..Q..]..9.&w.....5..`4.e..0......Q8d.O..p.I..8.j.r..?...$
a....^.5...&.|.....4......&..z.&I&2.c]L..(.........~....8..a.xc.....7t...O.z.............m....\.~{............}I..i?.M.H.a....[/m]..:.P....B.y........;...m5..|..ZEau.....hBC}0..D..a....... .....Y.....^...6.
...../m...V=Ob.D./c..)..G...hH..h...1.$].@k........{......8...?..... ..

谢谢。

【问题讨论】:

    标签: javascript ajax curl http-headers


    【解决方案1】:

    您的 curl 命令令人困惑。特别是网址。所以我在命令中使用了一个虚拟 url。

    curl -X POST -H "dataType: html" -d "/notam/p ib" -H "indicador: TRUE" http://url/
    

    我把你的单引号换成了双引号,还调整了-H前面的空格。

    【讨论】:

    • 我意识到数据真正所在的url是186.153.175.229/portal/notam,带有参数-H“indicador:TRE”的东西是TRE是来自阿根廷trelew机场的指标,并发送到邮政。现在随着您的改进,我收到错误 405 。我还注意到,当我使用页面生成 POST 时,在wireshark 中看到的 POST 中显示 POST /notam/pib HTTP/1.1,而我使用 curl 生成的 POST 显示 POST /portal/notam HTTP/1.1 i认为它必须与我发布的 ajax 方法有关,但不知道如何让 curl 复制它。谢谢!
    【解决方案2】:

    我已经用这个解决了我的问题

    curl -X POST -H "X-Requested-With: XMLHttpRequest"  -d "indicador=TRE"http://186.153.175.229/notam/pib 
    

    我的问题是我将 ajax 查询的 url 部分作为 -d 发送,实际上是来自主机 ip 的相对路径。还发现这个http://www.laktek.com/2012/03/12/curl-tips-for-daily-use/ 很有帮助。

    我无法在 windows 下编译 curl,所以我使用从 curl 中学到的知识,使用 Qt c++ 使用以下代码制作 http POST。

        QNetworkAccessManager *nwam = new QNetworkAccessManager;
        QNetworkRequest request(QUrl("http://186.153.175.229/notam/pib"));    
        QUrl URL;
        QByteArray postData;
        URL="http://186.153.175.229/portal/notam";
        request.setRawHeader( "X-Requested-With"," XMLHttpRequest" );
        QString postKey = "indicador=TRE";
        postData.append(postKey);
        QNetworkReply *reply = nwam->post(request,postData);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-14
      • 1970-01-01
      • 1970-01-01
      • 2018-06-17
      • 2013-08-19
      • 2023-03-16
      • 1970-01-01
      相关资源
      最近更新 更多