检查Python是否写了目标文本

Check if Python has written the targeted text

我正在编写代码以从大量网页中删除可见文本的选定部分。这是其中的一部分:

                divTag = soup.find_all("div", {'id':'articleBody'})
                for tag in divTag:
                    pTags = tag.find_all("p") 
                    for tag in pTags:
                        print >>f, tag.text

如何检查 Python 是否找到并写入了目标文本 ,如果抓取,则将 link 放在一边(到列表)没有成功?

我在这里没有找到答案,我也不知道去哪里看文档。

这是了解 python 是否找到您要查找的文本的替代方法:

import requests
from bs4 import BeautifulSoup

urls = ['https://www.google.com']
for i in range(len(urls)):
    r = requests.get(urls[i])
    soup = BeautifulSoup(r.content, 'lxml')
    items = soup.find_all('p')
    for item in items:
        if "2016 - Privacidad - Condiciones" in item.text:
            print "Python has found the targeted text"

如果python没有找到text,您需要使用remove()方法。