Using BeautifulSoup to search html for string

后端 未结 4 1284
予麋鹿
予麋鹿 2020-11-30 01:20

I am using BeautifulSoup to look for user entered strings on a specific page. For example, I want to see if the string \'Python\' is located on the page: http://python.org<

4条回答
  •  無奈伤痛
    2020-11-30 01:44

    The following line is looking for the exact NavigableString 'Python':

    >>> soup.body.findAll(text='Python')
    []
    

    Note that the following NavigableString is found:

    >>> soup.body.findAll(text='Python Jobs') 
    [u'Python Jobs']
    

    Note this behaviour:

    >>> import re
    >>> soup.body.findAll(text=re.compile('^Python$'))
    []
    

    So your regexp is looking for an occurrence of 'Python' not the exact match to the NavigableString 'Python'.

提交回复
热议问题