【问题标题】:Use Beautiful Soup to scrape all questions a person has answered on Quora使用 Beautiful Soup 抓取一个人在 Quora 上回答的所有问题
【发布时间】:2020-11-12 21:17:33
【问题描述】:

我如何编写漂亮的汤来抓取特定用户已回答的所有问题?

输入:
作者网址
例如:https://www.quora.com/profile/AUTHOR/answers)

输出:
第 1 列:作者已回答的问题
示例:“Lorem Ipsum 问题”

第 2 列:已回答问题的 URL
示例:https://www.quora.com/lorem-ipsum-question

第 3 列:已回答问题的 URL
示例:https://www.quora.com/lorem-ipsum-question

【问题讨论】:

    标签: web-scraping beautifulsoup quora


    【解决方案1】:

    此脚本将打印页面上找到的所有答案/网址。还有无限滚动向https://www.quora.com/graphql/gql_para_POST?q=UserProfileAnswersMostRecent_RecentAnswers_Query 发出 POST 请求,但我无法从中获取数据(您可以在开发者工具 -> 网络选项卡中看到它):

    import re
    import json
    import requests
    
    
    url = 'https://www.quora.com/profile/Nana-Bello-Shehu/answers'
    html_data = requests.get(url).text
    
    d = re.findall(r'window\.ansFrontendGlobals\.data\.inlineQueryResults\.results\[".*?"\] = ("{.*}");', html_data)[-1]
    d = json.loads(json.loads(d));
    
    for e in d['data']['user']['recentPublicAndPinnedAnswersConnection']['edges']:
        if e['node']['__typename'] != 'Answer':
            continue
    
        q = json.loads(e['node']['question']['title'])
        title = q['sections'][0]['spans'][0]['text']
        u = 'https://www.quora.com' + e['node']['question']['url']
        print('{:<90} {}'.format(title, u))
    

    打印:

    Do pictures speak louder than words?                                                       https://www.quora.com/Do-pictures-speak-louder-than-words
    Does true love exist?                                                                      https://www.quora.com/Does-true-love-exist-8
    What picture made your blood boil?                                                         https://www.quora.com/What-picture-made-your-blood-boil
    What are the before and after pics of people who are drug addicts for several years?       https://www.quora.com/What-are-the-before-and-after-pics-of-people-who-are-drug-addicts-for-several-years
    What was the funniest thing you saw/heard today?                                           https://www.quora.com/What-was-the-funniest-thing-you-saw-heard-today
    Are there any truly selfless acts, motives, or people?                                     https://www.quora.com/Are-there-any-truly-selfless-acts-motives-or-people
    Which famous person in history who is idolized, was actually a horrible person?            https://www.quora.com/Which-famous-person-in-history-who-is-idolized-was-actually-a-horrible-person
    What is something that you read recently and is worth sharing?                             https://www.quora.com/What-is-something-that-you-read-recently-and-is-worth-sharing
    How do I get the attention of my crush?                                                    https://www.quora.com/How-do-I-get-the-attention-of-my-crush
    What are some heart touching stories of best friends?                                      https://www.quora.com/What-are-some-heart-touching-stories-of-best-friends
    

    【讨论】:

      【解决方案2】:

      我认为最简单的方法是使用 selenium:

      from selenium import webdriver
      from selenium.webdriver.common.keys import Keys
      driver = webdriver.Firefox(executable_path='c:/program/geckodriver.exe')
      import time
      url = 'https://www.quora.com/profile/Nana-Bello-Shehu/answers'
      
      driver.get(url)
      
      SCROLL_TIME = 2
      
      
      last_height = driver.execute_script("return document.body.scrollHeight")
      
      while True:
      
          driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
      
      
          time.sleep(SCROLL_TIME)
      
      
          new_height = driver.execute_script("return document.body.scrollHeight")
          if new_height == last_height:
              break
          last_height = new_height
      
      qbox = driver.find_elements_by_css_selector('.qu-pb--medium')
      for qb in qbox:
          print(qb.find_element_by_css_selector('span.qu-userSelect--text').text)
          print('https://www.quora.com' + qb.find_element_by_css_selector('a.q-box.qu-cursor--pointer.qu-hover--textDecoration--underline').get_attribute('href'))
          print('\n')
      

      输出:

      Do pictures speak louder than words?
      https://www.quora.comhttps://www.quora.com/profile/Nana-Bello-Shehu
      
      
      Does true love exist?
      https://www.quora.comhttps://www.quora.com/profile/Nana-Bello-Shehu
      
      
      What picture made your blood boil?
      https://www.quora.comhttps://www.quora.com/profile/Nana-Bello-Shehu
      
      
      What are the before and after pics of people who are drug addicts for several years?
      https://www.quora.comhttps://www.quora.com/profile/Nana-Bello-Shehu
      
      
      What was the funniest thing you saw/heard today?
      https://www.quora.comhttps://www.quora.com/profile/Nana-Bello-Shehu
      
      
      Are there any truly selfless acts, motives, or people?
      https://www.quora.comhttps://www.quora.com/profile/Nana-Bello-Shehu
      

      等等……

      此脚本滚动到页面末尾并复制所有问题。您可以尝试设置较低的 SCROLL_TIME 以使脚本更快,但有时脚本会在页面结束之前结束,滚动时间更短。

      注意:

      1. 你需要selenium
      2. 你需要火狐
      3. 您需要 geckodriver,现在脚本从 c:/program/geckodriver.exe 导入它,因此如果您将 geckodriver 添加到其他路径,您需要更改 executable_path

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-23
        • 1970-01-01
        • 2017-03-30
        • 1970-01-01
        • 2016-05-16
        • 2020-07-27
        相关资源
        最近更新 更多