如何测试网页是否包含某些文本

时间:2018-03-01 23:53:40

标签: vba

我正在尝试检测网页是否包含某些文字。例如,我想看看this web page是否包含以下短语:“这是我的代码”

我无法让它发现满足“If Then”条件。这是我正在尝试的:

Const READYSTATE_COMPLETE = 4
Declare Function SetForegroundWindow Lib "user32" _
Alias "SetForegroundWindow" (ByVal Hwnd As Long)As Long
' Declare Internet Explorer object
Dim IE As SHDocVw.InternetExplorer
 Dim strProgramName As String

Sub Main

   ' create instance of InternetExplorer
   Set IE = New InternetExplorer
   ' using your newly created instance of Internet Explorer

  With IE
      SetForegroundWindow IE.HWND
      .Visible = True
      .Navigate2 "https://stackoverflow.com/questions/38355762/how-do-i-modify-web-scraping-code-to-loop-through-product-bullets-until-it-finds"

  ' Wait until page we are navigating to is loaded
      Do While .Busy
      Loop
      Do
      Loop Until .readyState = READYSTATE_COMPLETE
      On Error Resume Next
         If Err Then

         Else

  End If

  Wait 2


If InStr(IE.document.body.innerHTML, "Here is my code") > 0 Then

MsgBox "Yessiree Bob"

Else

MsgBox "The text dosen't exist"

End If

    Set IE = Nothing
   ' Tidy Up
End With
End Sub

我也试过了:

 FindText = InStr(1, IE.document.body.innerHTML, "Here is my code")

 If FindText > 0 Then

 msg = IE.document.body.innerHTML
 If InStr(msg, "Here is my code") > 0 Then

但没有任何作用。我查看了Stack Overflow,但找不到这个确切的问题。

提前致谢!

1 个答案:

答案 0 :(得分:1)

使用:

class CurrentBranchDefault:
    def set_context(self, serializer_field):
        self.user = serializer_field.context['request'].user
        self.branch = self.user.userprofile.selected_branch

    def __call__(self):
        return self.branch

    def __repr__(self):
        return unicode_to_repr('%s()' % self.__class__.__name__)


class StaffOrderSerializer(serializers.ModelSerializer):
    branch = serializers.HiddenField(default=CurrentBranchDefault())