Search code examples
pythonpython-3.xbeautifulsouprequestwebrequest

How to search multiple strings match in a webpage using python request


I wanted to improve the snippet below by passing in multiple strings to search ("English Subtitles", "1080", "2021") in the webpage. Currently, it works with single string search.

import requests
url = 'http://www.allyoulike.com/'
r = requests.get(url)

singlesearchstring = "2021"

multiplesearchstring = "English Subtitles", "4080", "2021"

if (stringtosearch) in r.text:
    print ('Found ',singlesearchstring )
else:
    print ('Not Found ', singlesearchstring)

Wanted Output:

Search Results:
  English Subtitles - Found
  4080 - Not Found
  2021 - Found 

Solution

  • you can do:

    [(q, 'Found' if q.lower() in r.text.lower() else 'Not Found') for q in queries]
    
    import requests
    
    queries = ["English Subtitles", "4080", "2021"]
    
    
    def main(url):
        r = requests.get(url)
        for q in queries:
            q = q.lower()
            if q in r.text.lower():
                print(q, 'Found')
            else:
                print(q, 'Not Found')
    
    
    main('http://www.allyoulike.com/')
    

    Updated Answer:

    import requests
    from bs4 import BeautifulSoup
    import re
    from pprint import pp
    
    queries = ["English Subtitles", "4080", "2021"]
    
    
    def get_line(q, soup):
        return [x for x in soup.findAll(text=re.compile('{!s}'.format(q)))]
    
    
    def main(url):
        r = requests.get(url)
        soup = BeautifulSoup(r.text, 'lxml')
        goal = [(q, 'Found', get_line(q, soup)) if q.lower()
                in r.text.lower() else (q, 'Not Found') for q in queries]
    
        pp(goal)
    
    
    main('http://www.allyoulike.com/')