【问题标题】:Get source code containing all information in google feedback box?获取包含谷歌反馈框中所有信息的源代码?
【发布时间】:2016-08-04 07:27:31
【问题描述】:

我想使用 BeautifulSoup 解析 Google 反馈卡中的所有文本。

对于查询“define apple”,Google 会显示带有 drop down menu。我想解析there 中的所有文本。

类似的问题是here 但该解决方案不会解析整个框(下拉后),因为requests.get(url) 获取的源代码不包含该信息。

有没有一种方法可以在没有 selenium 的情况下获得整个源代码。

【问题讨论】:

    标签: python beautifulsoup python-requests


    【解决方案1】:

    您确实使用 requests 获取了所有源,您可能遇到的唯一问题是某些输出是图像:

    headers = {
        "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36",
    }
    
    soup = BeautifulSoup(requests.get("https://www.google.ie/search?q=define+apple", headers=headers).content, "lxml")
    
    
    div = soup.select("div.lr_dct_ent span")
    desc = "\n".join(sp.text.strip("; ") for sp in div)
    img = soup.select_one("div.lr_dct_ent").find_next("img")
    print(desc)
    print(img["src"])
    print "\n".join(d.text for d in  soup.select("div.vk_sh.vk_gy"))
    

    这给了你:

    apple
    ˈap(ə)l/
    ˈap(ə)l
    
    noun
    noun: apple
    plural noun: apples
    noun: apple tree
    plural noun: apple trees
    the round fruit of a tree of the rose family, which typically has thin green or red skin and crisp flesh.
    used in names of unrelated fruits or other plant growths that resemble apples in some way, e.g. custard apple, oak apple.
    the tree bearing apples, with hard pale timber that is used in carpentry and to smoke food.
    Old English æppel, of Germanic origin; related to Dutch appel and German Apfel .
    data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
    Origin
    Use over time for: apple
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-04
      • 2017-10-14
      • 2019-09-10
      • 1970-01-01
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多