【问题标题】:How to search multiple strings match in a webpage using python request如何使用python请求在网页中搜索多个字符串匹配
【发布时间】:2021-10-12 18:15:19
【问题描述】:

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

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 

【问题讨论】:

    标签: python python-3.x beautifulsoup request webrequest


    【解决方案1】:

    你可以这样做:

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

    【讨论】:

    • 是否可以只返回找到字符串的整行?
    • @rbutrnz 检查更新的答案,并查看How to Ask
    猜你喜欢
    • 2021-10-12
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    相关资源
    最近更新 更多