Я хочу игнорировать контент, который присутствует в определенном классе (класс нижнего колонтитула), но очищать весь другой контент, присутствующий на веб-странице. Я не могу найти решение в сети для того же.
from bs4 import BeautifulSoup
from urllib.request import urlopen
import re
url_list=['https://www.aarp.org']
myWords_External=[]
#Below we have used BeautifulSoup library to scrape the webpages
for url in url_list:
myWords=[]
myWords_internal=[]
r = requests.head(url)
code=r.status_code
if code == 200:
print(url)
answer = requests.get(url)
html = urlopen(url)
soup = BeautifulSoup(html, "html5lib")
text = (''.join(s.findAll(text=True))for s in soup.findAll({'h1' : True,'h2' : True,'h3' : True,'h4' : True,'p' : True}))
for y in text:
g=y.lower()
z=re.split('\W+',g)
texts= " ".join([word for word in z if word not in string.punctuation])
tokens= re.split('\W+',texts)
texts2= " ".join([ps.lemmatize(word) for word in tokens if word not in stopwords])
tokens= re.split('\W+',texts2)
texts3= " ".join([word for word in tokens if word.isnumeric() == False])
tokens= re.split('\W+',texts3)
texts4=" ".join([word for word in tokens if word != " "])
tokens= re.split('\W+',texts4)
texts5=" ".join([word for word in tokens if word != ""])
tokens= re.split('\W+',texts5)
str_list = list(filter(None, tokens))
myWords_internal.append(str_list)
myWords = [item for sublist in myWords_internal for item in sublist]
myWords_External.append(myWords)
print(myWords_External)
I tried below variation but it didn't work
text = (''.join(s.findAll(text=True))for s in soup.findAll({'name-of class':False,'h1' : True,'h2' : True,'h3' : True,'h4' : True,'p' : True}))