如何使用 python 请求在网页中搜索多个字符串匹配

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

我想通过传入多个字符串来改进下面的代码段以在网页中搜索(“英文字幕”、“1080”、“2021”)。目前,它适用于单个字符串搜索。

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)

想要的输出:

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

你可以做到:

[(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/')

更新答案:

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/')