0

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

4

1 回答 1

7

我将逐步回答这些问题:

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)

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

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

注意

  1. 为您的 chrome 版本安装Chrome 驱动程序

  2. 使用pip install -U 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)
于 2018-12-25T16:58:24.650 回答