自动构建搜索词

时间:2018-10-22 02:36:38

标签: python-3.x web-scraping screen-scraping

我正在做一些抓取工作,但我希望自动构建大量关键字。我设计的一种既方便又不方便的方法如下:

def build_search_terms():
    words1 = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
    words2 = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
    for word in words2:
        result = words1[0] + word
        words2.pop(0)
        search_for(result)

我想做的是创建一个函数,将aa弹出到az,然后将ba弹出到bz,然后将ca弹出到{ {1}},依此类推。

以前有人解决过这个问题吗?

有更有效的方法吗?

1 个答案:

答案 0 :(得分:0)

您将获得所需的输出,如下所示:

def build_search_terms():
    words_list = []
    words = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
    for i in words:
        for j in words:
            yield i + j

并将其用作

for word in build_search_terms():
    print(word)

def build_search_terms():
    words = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
    return (i +j for i in words for j in words)

并将其用作

words = build_search_terms()
print(next(words))  # 'aa'
print(next(words))  # 'ab'
print(next(words))  # 'ac'
....