【问题标题】:Automatically scrape multiple questions from quora with a specific tag?使用特定标签自动从 quora 中抓取多个问题?
【发布时间】:2018-12-18 12:31:01
【问题描述】:

我想从 Quora 中抓取与某些特定主题相关的问题,该主题有超过 4 个答案左右。

我想找到

a) 答案数

b) 与每个问题相关的标签

这是我的程序:

res=requests.get("https://www.quora.com/How-does-Quora-automatically-know-what-tags-to-put-for-a-question")

soup=BeautifulSoup(res.text, 'lxml')
# All the ans inside pagedlist_item
ans=soup.find_all('div', {'class' : 'pagedlist_item'})


#Question Name inside question_text_edit
qname=soup.find('div', {'class' : 'question_text_edit'})
#qnam=soup.find('div', {'class' : 'question_text_edit'})


#Tag of Question
tags=soup.find('div', {'class' : 'QuestionTopicHorizontalList TopicList'})



#checking to see if "TV" is the tag of the question in the current webpage 
#Also, checking if no. of answers of the given question >=4, if yes then print the question
#logic for checking the conditions
no_ans=0;
if "TV" in tags.text:
    print(i.text)
    for a in ans:
        no_ans=no_ans+1
    if no_ans>=4:
        print(qname.text)

我想搜索许多带有标签TV的此类页面,然后对这些页面执行检查以满足上述条件。

检查条件的逻辑出现在代码的末尾。但是,这仅适用于地址在requests.get("") 函数内的网页中的一个问题

如何让代码自动迭代多个带有“TV”标签的网页(多个问题),而不是将单个网页地址传递给requests.get("") 函数?

另外,我想收集多个问题(多达 40 个左右)。

【问题讨论】:

  • 先看看curl
  • 是的,你也可以开始看看 BeautifulSoup
  • @AjaySinghNegi 该方法正如我所描述的那样:找出您(人类)会手动执行的操作以获取所有这些页面的列表,然后自动化构建该列表的过程。例如,如果您作为人类必须单击 Quora 文章上的 [TV] 标签以获取有关电视的所有 Quora 问题的列表,那么您现在知道您必须构建一个机器人来为您单击该标签.对我来说似乎很简单,而且我不知道您在这里寻求帮助的原因是什么。无论您作为一个人会手动执行此操作,都将其自动化。
  • @AjaySinghNegi 你不需要框架。只写代码。这个问题没有什么特别之处。这是日常编程。这主要是程序所做的事情,它们最初是为了什么而编写的:自动执行否则人类必须做的任务。
  • @DanBron 先生,感谢您的帮助。我希望现在回答这个问题可以帮助我消除“您已达到问题限制”。

标签: python web-scraping quora


【解决方案1】:

我会一步一步回答这些:

I want to search over many such pages which have the tag TV and then later perform the check over those pages to satisfy the above condition.

好吧,如果你想抓取多个这样的页面,你必须从有许多与该特定主题相关的问题的主题的根页面开始,然后开始抓取该根页面中列出的这些问题的链接。

Also, I want to scrape multiple questions(as many as 40 or so)

为此,您需要模拟滚动,以便在向下时找到越来越多的问题。

您不能直接使用RequestsBeautifulSoup 来执行模拟滚动操作等事件。这是我在 Python 中使用Selenium 库来满足您的要求的一段代码。

注意

  1. 安装Chrome driver for your chrome version

  2. 使用 pip install -U selenium 安装 selenium。

  3. 如果您使用的是 windows - executable_path='/path/to/chromedriver.exe'

此代码要求提供 2 个链接,然后开始抓取“问题、答案编号、标签、4 个答案”并将它们保存为 csv 格式。

Keys.PAGE_DOWN 用于模拟滚动按钮。 不同的详细信息已附加到row 列表并在最后保存 进入csv 文件。

另外,您可以更改no_of_pagedowns 变量的值以增加编号。的 你想要的卷轴。

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import csv


with open('submission.csv','w') as file:
    file.write("Question,No. of answers,Tags,4 answers")

link1 = input("Enter first link")
#link2 = input("Enter second link")
manylinks = list()
manylinks.append(link1)
#manylinks.append(link2)
for olink in manylinks:
    qlinks = list()    
    browser = webdriver.Chrome(executable_path='/Users/ajay/Downloads/chromedriver')
    browser.get(olink)
    time.sleep(1)
    elem = browser.find_element_by_tag_name("body")


    no_of_pagedowns = 50
    while no_of_pagedowns:
        elem.send_keys(Keys.PAGE_DOWN)
        time.sleep(0.2)
        no_of_pagedowns-=1
    post_elems =browser.find_elements_by_xpath("//a[@class='question_link']")
    for post in post_elems:
        qlink = post.get_attribute("href")
        print(qlink)
        qlinks.append(qlink)

    for qlink in qlinks:

        append_status=0

        row = list()

        browser.get(qlink)
        time.sleep(1)


        elem = browser.find_element_by_tag_name("body")


        no_of_pagedowns = 1
        while no_of_pagedowns:
            elem.send_keys(Keys.PAGE_DOWN)
            time.sleep(0.2)
            no_of_pagedowns-=1


        #Question Names
        qname =browser.find_elements_by_xpath("//div[@class='question_text_edit']")
        for q in qname:
            print(q.text)
            row.append(q.text)


        #Answer Count    
        no_ans = browser.find_elements_by_xpath("//div[@class='answer_count']")
    #    print("No. of ans :")
        for count in no_ans:
    #        print(count.text)
            append_status = int(count.text[:2])

            row.append(count.text)

        #Tags
        tags = browser.find_elements_by_xpath("//div[@class='header']")
    #    print("\nTag :")
        tag_field = list()
        for t in tags:
            tag_field.append(t.text)
    #        print(t.text,'\n')
        row.append(tag_field)


        #All answers
        all_ans=browser.find_elements_by_xpath("//div[@class='ui_qtext_expanded']")
        i=1
        answer_field = list()
        for post in all_ans:
            if i<=4:
                i=i+1
    #            print("Answer : ")
    #            print(post.text)
                answer_field.append(post.text)
            else:
                break   
        row.append(answer_field)


        print('append_status',append_status)

        if append_status >= 4:
            with open('submission.csv','a') as file:
                writer = csv.writer(file)
                writer.writerow(row)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    • 2020-11-12
    • 2019-02-13
    • 2020-08-28
    • 2020-07-22
    相关资源
    最近更新 更多