代码之家  ›  专栏  ›  技术社区  ›  farhan jatt

将fakeuseragent添加到peoplealsosk模块

  •  1
  • farhan jatt  · 技术社区  · 4 年前

    我想在谷歌上搜索 “人们也会提问/回答” 。我用以下模块成功地完成了这项工作。

    pip install people_also_ask
    

    问题是库的配置使得没有人可以向谷歌发送许多请求。我想每天发送1000个请求,为了实现这一点,我必须将fake_useragent添加到模块中。我尝试了很多,但当我尝试添加虚假的用户代理到标题时,它会出错。我不是职业选手,所以我自己一定做错了。有人能帮我把fake_useragent添加到模块(people_also_ask)中吗。这是获取问题/答案的工作代码。

    from encodings import utf_8
    import people_also_ask as paa
    from fake_useragent import UserAgent
    ua = UserAgent()
    while True:
    
        input("Please make sure the queries are in \\query.txt file.\npress Enter to continue...")
        try:
            query_file = open("query.txt","r")
            queries = query_file.readlines()
            query_file.close()
            break
        except:
            print("Error with the query.txt file...")
    
    
    for query in queries:
    
        res_file = open("result.csv","a",encoding="utf_8")
    
        try:
            query = query.replace("\n","")
        except:
            pass
    
        print(f'Searching for "{query}"')
        
        questions = paa.get_related_questions(query, 14)
        questions.insert(0,query)
    
        print("\n________________________\n")
        main_q = True
        for i in questions:
    
            i = i.split('?')[0]
            
            try:
                answer = str(paa.get_answer(i)['response'])
                if answer[-1].isdigit():
                    answer = answer[:-11]
                print(f"Question:{i}?")
            except Exception as e:
                print(e)
    
            print(f"Answer:{answer}")
    
    
            if main_q:
                a = ""
                b = ""
                main_q = False
                
            else:
                a = "<h2>"
                b = "</h2>"
    
            res_file.writelines(str(f'{a}{i}?{b},"<p>{answer}</p>",'))
    
            print("______________________")
    
        print("______________________")
        res_file.writelines("\n")
    
        res_file.close()
    
    print("\nSearch Complete.")
    input("Press any key to Exit!")
    
    0 回复  |  直到 4 年前
        1
  •  1
  •   QuantumMecha    4 年前

    这违背了谷歌的服务条款,也违背了 people_also_ask 包裹这个答案仅用于教育目的。

    你问过 为什么? fake_useragent 被阻止工作。它并没有被阻止工作,但 people_also_ask 包根本没有实现任何调用来利用 fake_useragent 方法。你不能只导入一个包,然后期望另一个包开始使用它。你必须手动使包协同工作。

    要做到这一点,您必须对这两个包的工作原理有一些了解。看看 the source code 你会发现你可以很容易地让他们一起工作。只需在中替换常量标头 people_also_ask 其中一个由 fake_useragent 在您请求任何数据之前。

    paa.google.HEADERS = {'User-Agent': ua.random} # replace the HEADER with a randomised HEADER from fake_useragent
    questions = paa.get_related_questions(query, 14)
    

    paa.google.HEADERS = {'User-Agent': ua.random} # replace the HEADER with a randomised HEADER from fake_useragent
    answer = str(paa.get_answer(i)['response'])
    

    注意: 并非所有用户代理都能工作。谷歌不会根据用户代理给出相关问题。这不是任何一方的错 fake_useragent ,或 people_also_ask package

    为了在一定程度上缓解这个问题,请务必致电 ua.update() 你也可以使用 PR #122 属于 fake_useragents 只选择最新的用户代理的一个子集,这些代理更有可能工作,尽管您仍然会得到一些遗漏的查询。people_also_ask包没有绕过或绕过谷歌的这一限制是有原因的