查找具有特定字符串/字符的标记,如“quot;”&引用;在python中标记的文本字段中

查找具有特定字符串/字符的标记,如“quot;”&引用;在python中标记的文本字段中,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我正试图抓取一个网站进行一些处理- 仅当文本部分中存在“?”时,才尝试查找“标记”。 为此,我转到这里,编写了这样一个函数 def find_quest(tag): text = tag.text str_text = str(text) bool_ = False for i in range(len(str_text)): if str_text[i] == "?": bool_ = Tru

我正试图抓取一个网站进行一些处理-

仅当文本部分中存在“?”时,才尝试查找“标记”。 为此,我转到这里,编写了这样一个函数

def find_quest(tag): 
    text = tag.text 
    str_text = str(text) 
    bool_ = False 
    for i in range(len(str_text)): 
        if str_text[i] == "?":
            bool_ = True 
            break 
    return bool_

但仍然无法提取标签-它将所有内容作为输出,而不仅仅是选定的标签。有人知道怎么做吗? 完整代码

def find_quest(标签):
text=tag.text
str_text=str(text)
布尔=假
对于范围内的i(len(str_text)):
如果str_text[i]=“?”:
布尔=真
打破
返回布尔_
def get_bs4_标签(子项列表):
标签列表=[]
对于范围内的i(len(儿童列表)):
字符串i=str(子项列表[i])
如果子元素列表[i]==“bs4.element.Tag”或子元素列表[i]==”:
标记列表。附加(i)
返回标签列表
def下载页面(链接):
页面=请求。获取(链接)
soup=BeautifulSoup(page.content,'html.parser')
children_list=[str(type(item))表示列表中的项目(soup.children)]
tag_i_列表=get_bs4_标签(子列表)
对于标签列表中的i:
html=列表(soup.children)[i]
x=列表(html.find_all(find_quest))
下载页(“https://www.spykar.com/faq")

这可能会满足您的需求:

import requests
from bs4 import BeautifulSoup

url = 'https://www.spykar.com/faq'
page = requests.get(url)
soup = BeautifulSoup(page.text, 'html.parser')
content = soup.find('div', {'id': 'content'})
rows = content.find_all('div', {'class': 'row'})
for row in rows:
    texts = row.find_all('span')
    for text in texts:
        if '?' in text.text:
            print(text.text)

使用正则表达式。并找到包含
的文本,然后获取父级

import re
import requests
from bs4 import BeautifulSoup
page = requests.get("https://www.spykar.com/faq")
soup = BeautifulSoup(page.content, 'html.parser')
for item in soup.find_all(text=re.compile("\?$")):
     print(item.parent)
输出

<span>Q: Do I have to register to shop at www.spykar.com?</span>
<span>Q: Do I have to disclose my e-mail id and phone number for registration?</span>
<span>Q: How secure is shopping with www.spykar.com or Is my personal Information secure?</span>
<span>Q: Are there any charges for registration?</span>
<span>Q: How do I get a new password?</span>
<span>Q: Can I edit my personal information?</span>
<span>Q: What kind of payment option does www.spykar.com offer?</span>
<span>Q: How do I know of www.spykar.com latest news and promotions?</span>
<span>Q: I have received a few emails from www.spykar.com. What are Orders Received, Orders Processed and Payment Receipt?</span>
<span>Q: I'm missing an item from my order, what do I do?</span>
<span>Q: What do I do if I receive a faulty item in my order?</span>
<span>Q: I have an incorrect item in my order, what do I do?</span>
<span>Q: Can I change my order after I've placed it?</span>
<span>Q: How long would it take for me to receive the refund of the returned/cancelled product?</span>
<span>Q: Within how many days can I return the product?</span>
<span>Q: How can I raise a return request on Spykar website?</span>
<span>Q: How do I return multiple products from a single order?</span>
<span>Q: Does Spykar pick up the product I want to return from my location?</span>
<span>Q: Why was my return request declined?</span>
<span>Q: Why did the return pickup of my product fail?</span>
<span>Q: How do I request a pickup again if the first attempt failed?</span>
<span>Q: How long would it take for me to receive the refund of the returned/cancelled product?</span>
<span>Q: Can I cancel my order after I've placed it?</span>
<span>Q: How long would it take for me to receive the refund of the returned/cancelled product?</span>
<span>Q: How long would it take for me to receive the refund of the returned/cancelled product?</span>
<span>Q: How can I exchange products I bought from www.Spykar.com?</span>
<span>Q: When will I receive the products I have ordered?</span>
<span>Q: Are there any delivery charges?</span>
<span>Q: Does Spykar deliver products outside India?</span>
<span>Q: How can I get my order delivered faster?</span>
<span>Q: What is ActifClub?</span>
<span>Q: What happens in ActifClub?</span>
<span>Q: What does a customer need to do to become an ActifClub member?</span>
<span>Q: Is there any joining fee?</span>
<span>Q: When will I get the points credited?</span>
<span>Q: What is the value of the points?</span>
<span>Q: How do I get my Rewards Points?</span>
<span>Q: How can I redeem the points?</span>
<span>Q: Where can I check number of points I have?</span>
<span>Q: If I forget to collect points, what should I do?</span>

这个
get_bs4_tag
return是什么?@Vitor bs4.element.tag-question-updated只是为了确保,您正在尝试删除该网站中的所有常见问题解答?谢谢@Vitor,但我需要“尝试查找”标记,只有在文本部分出现“?”时才需要
print(item.parent.text)