使用特定标签自动从 quora 中抓取多个问题?

Automatically scrape multiple questions from quora with a specific tag?

我想从 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个左右)。

我会逐步回答这些问题:

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)