【问题标题】:I'm scraping a FAQ pages and I need to find which tag has answers in FAQ pages我正在抓取一个常见问题页面,我需要在常见问题页面中找到哪个标签有答案
【发布时间】:2018-02-13 05:39:04
【问题描述】:
import requests
from bs4 import BeautifulSoup
import numpy as np
import pandas as pd                     
import re
req = requests.get('https://www.godrejproperties.com/nricorner/nri-faqs')
soup = BeautifulSoup(req.text, "html5lib")

ist1=[]
for elem in soup(text=re.compile(r'\s*((?:how|How|Can|can|what|What|where|Where|describe|Describe|Who|who|When|when|Why|why|Should|should|is|Is|I|Do|do|Are|are|Will|will)[^.<>?]*?\s*\?)')):
    print elem.parent
    list1.append(elem.parent)

x=str(list1[1])
tag=x[x.find("<")+1:x.find(">")]
print tag

Ques = []
for header in soup.find_all(tag):
    list_=[header]
    ffff=re.findall(r'\s*((?:how|How|Can|can|what|What|where|Where|describe|Describe|Who|who|When|when|Why|why|Should|should|is|Is|I|Do|do|Are|are|Will|will)[^.<>?]*?\s*\?)',str(list_))
    #print(ffff)
    #print (len(ffff))
    if len(ffff)>0:
        Ques.append(ffff)
Ques = np.array(Ques)
print(Ques) 

同样,我需要在常见问题解答页面中找到答案,我需要创建一个算法,该算法将捕获包含哪个标签答案并获取其内容并将其保存在列表中。稍后我需要一对一的问答

【问题讨论】:

    标签: python pandas numpy web-scraping beautifulsoup


    【解决方案1】:

    您可以使用xpath 获取详细信息。 正如您所看到的 html 结构,所有问题和答案都在手风琴中。 那么基本上我们需要通过属性来遍历它。 对于直接答案,我们可以使用以下 xpath 位置

    //*[@class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom"]

    但您需要聪明,因为这可能会导致其他手风琴进入您捕获的数据,因此请根据问题 id 验证数据,这也反映在答案 id 中。

    //*[@class="ui-accordion-header ui-state-default ui-corner-all ui-accordion-icons"]

    你也可以使用 xpath 或 css 选择器例如:

    甚至通过article

    【讨论】:

    • 感谢ans shiv,但我想创建一个算法来找到答案标签,而不管我给出的常见问题解答网址是什么(如果我给出其他网址,不仅是特定的一个网址,我的日志应该捕获那些答案以及)
    • @MukeshMarimuthu 你想概括刮擦算法吗?
    • 是的,我需要概括算法以抓取可用于至少少数网站的常见问题页面
    • @MukeshMarimuthu 然后您可能需要以将输入作为 Xpath 或 CSS 选择器并返回可用标志内的数据的方式创建算法,然后分离函数来处理数据,因为它看起来不同的网站会有不同的 html 结构,如果都相同,那么它们是没有问题的
    • Ans=[] for header in soup.find_all(tag): list_=[header] ffff=re.findall(r'\s*((?:how|How|Can|can| what|What|where|Where|describe|Describe|Who|who|When|when|Why|why|Should|should|is|Is|I|Do|do|Are|are|Will|will)[^.?]*?\s*\?)',str(list_)) #print(ffff) #print (len(ffff)) if len(ffff)>0: a=header.find_next('p') # print(a.text) x=a.text c=[x] x1 = [i.replace('\n', "").replace('\t', '') for i in c] Ans.append (x1) Ans = np.array(Ans) print(Ans)
    猜你喜欢
    • 1970-01-01
    • 2017-04-28
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 2011-05-07
    • 2022-11-11
    • 2021-06-21
    • 1970-01-01
    相关资源
    最近更新 更多