【问题标题】:VBA Winhttp URLVBA Winhttp 网址
【发布时间】:2016-12-25 20:43:29
【问题描述】:

我在以下 VBA 代码中使用 Winhttp,但无法使 URL 正常工作,但如果我将 URL 粘贴到浏览器中,它就可以正常工作。任何想法将不胜感激:

Sub WinHttp()

Dim arr() As String
Dim pos As Integer
Dim used As Range

Dim url, resp As String
Dim req As New WinHttpRequest
Dim n As Integer

r = ActiveSheet.UsedRange.Rows.Count

For n = 4 To r

    Worksheets("Sheet0").Activate
    pn = Cells(n, 2).Value
    UnitPrice = (Cells(n, 4).Value) * 1

    url = "https://www.google.com/search?q=DNR-12-1G+$4,250"

    req.Open "GET", url, False
    req.send

    resp = req.ResponseText

    Debug.Print resp

    arr = split(resp)

    arr = Filter(arr, "$")

Next n

End Sub

【问题讨论】:

  • 当我把它放在一个网络调试器中时,护目镜返回了错误 302 和一个不错的网页,但是因为它是一个错误,所以 XMLHTTP 没有显示它。但是 302 说它已经移动并且有重定向。我还在q= 之后使用VBScript Escape 函数转义了你的链接,它给出了这个DNR-12-1G+%244%2C250
  • 302 已移动 HEAD>

    302 Moved

    文档已移动google.com.au/…>。
  • 如果你把它换成你当地的护目镜。如果我输入goggle.com.au,它就可以工作。

标签: excel vba winhttp


【解决方案1】:

谢谢!出现“req.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"

成功了。

【讨论】:

  • 嘿,我认为这应该是评论而不是答案,但不客气。
【解决方案2】:

刚刚在 Excel 2013 中测试了以下代码,运行良好:

Sub Test()
    Dim req As New WinHttpRequest
    Dim url, resp As String

    url = "https://www.google.com/search?q=DNR-12-1G+$4,250"

    req.Open "GET", url, False
    req.send

    resp = req.ResponseText
    Debug.Print resp
End Sub

服务器的响应是

<!DOCTYPE html><html lang=en><meta charset=utf-8><meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width"><title>Error 403 (Forbidden)!!1</title><style>*{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/
googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}</style><a href=//www.google.com/><span id=logo aria-label=Google></span></a><p><b>403.</b> <ins>That’s an error.</ins><p>Your client does not have permission to get URL <code>/search?q=DNR-12-1G+%244,250&amp;gws_rd=cr&amp;ei=IDVgWJ2dDomnsgGfy4u4Dw</code> from this server.  (Client IP address: 194.28.248.113)<br><br>
Please see Google's Terms of Service posted at http://www.google.com/terms_of_service.html
<BR><BR><P>If you believe that you have received this response in error, please <A HREF="https://www.google.com/support/contact/user?hl=en">report</A> your problem. However, please make sure to take a look at our Terms of Service (http://www.google.com/terms_of_service.html). In your email, please send us the <b>entire</b> code displayed below.  Please also send us any information you may know about how you are performing your Google searches-- for example, "I'm using the Opera browser on Linux to do searches from home.  My Internet access is through a dial-up account I have with the FooCorp ISP." or "I'm using the Konqueror browser on Linux to search from my job at myFoo.com.  My machine's IP address is 10.20.30.40, but all of myFoo's web traffic goes through some kind of proxy server whose IP address is 10.11.12.13."  (If you don't know any information like this, that's OK.  But this kind of information can help us track down problems, so please tell us what you can.)</P><P>We will use all this information
 to diagnose the problem, and we'll hopefully have you back up and searching with Google again quickly!</P>
<P>Please note that although we read all the email we receive, we are not always able to send a personal response to each and every email.  So don't despair if you don't hear back from us!</P>
<P>Also note that if you do not send us the <b>entire</b> code below, <i>we will not be able to help you</i>.</P><P>Best wishes,<BR>The Google Team</BR></P><BLOCKQUOTE>/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/<BR>
Kjm9A1b-agG1uA7g8B-cYcEbbyIbP2Zi4vVoYweB2AoPwPBt3<BR>
bmAY9hROWVTpQsuAA6kmBSVN141uaLrXs0m8ub4wC4dsCpPaQ<BR>
tuOQKksiTpmW8fSvzTxDiDtJ30yguDPQyBEfvx1txlHQiTfk-<BR>
lIVTsj7H3uWatFa4O9Zuxs7LvBBfCxrR69HFHpJpXWkDECkrY<BR>
AeRa1LGcxiPWYskIMweGWKTdkbdmf1q1hcmvDUOL457j5QPNx<BR>
NgwnPMBejraU_xbwSzoWNx2z7SfDmbPnUahlv69y-omfiMhp8<BR>
s1LQK90rg5hYAgbLmoVjMHMZ0WeRoay-XoB1oKQzff-nnkAD0<BR>
fGK074ZB-7MQM3oh-eX0KIwyThj-JwXGJEPRk2pwTJenH7RB1<BR>
9XzB2G-GiIIMoyVzdu7WUyd-Y9ZUJO4Sk8v6AXB8qtT7BUp2t<BR>
gnj2P_8Ve6uLta3Q9QPFH-XFGXJiHXL-xr-wQ4yz-0vh1wpEi<BR>
tYmc7dchSKFtzZfXPp0fGFYaD8My9GscqRNif8Yk35AEAzTJW<BR>
qoK-GRSDdSn4Xx26BXDkd_8-XvcxlCrW9n9uS06ksGBcNpk-z<BR>
DDr3fM-hXjTTFutoSYSNQEi5Tz8CgRFm11MEZT7vBPTvE1aiW<BR>
owzBaftrSrS9Kipke0HfZq6LWh0gBRvCF-fAXqHcjjt8fXCft<BR>
Jrxwv08vYmKxD_HLJkdpR1gxrW-JphLVAZ9ELeALfsi7fc74z<BR>
fvs63_f1mhzMBqjVK86Y1RwggmVeeRv6R0ClCaZT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ck6bsnfEzvVOCqO0qdXYpFib<BR>
4FwWT591tnyd<BR>
+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+<BR></BLOCKQUOTE>


  <ins>That’s all we know.</ins>

【讨论】:

  • 那是一个自定义的HTTP 403,我相信 OP 算作“无法使 URL 工作”。
  • 可能是。在这种情况下,这与 VBA/WinHTTP 关系不大。
  • 可以,但是not necessarily.
  • 好吧,您通常不应该使用 Google 来编写脚本,所以这就是在这种情况下导致 403 的原因。您可以通过req.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" 解决此问题,但您需要先查看服务条款。
  • @Bob:那是 Google 向您发送的错误:请求正常进行。如果你想欺骗他们的用户代理拦截器,你可以使用上面评论中的标题,但首先检查他们的服务条款。
猜你喜欢
  • 1970-01-01
  • 2013-02-17
  • 2017-05-22
  • 2014-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多