Чтение всех <p>в массив python BS4 - PullRequest
0 голосов
/ 03 апреля 2020

Эй, я пытаюсь прочитать все <p> теги в массив.

HTML Пример:

<p>To test web scrapers against invalid markup we suggest scraping this page that contains the following markup mistakes:</p>
<p>It's obvious that not every web publisher pays much attention to validity of his HTML code.</p>

Это должно привести к массиву вроде:

scraped = ["To test web scrapers against invalid markup we suggest scraping this page that contains the following markup mistakes:","It's obvious that not every web publisher pays much attention to validity of his HTML code."]

Мой текущий код:

class Webscraper:

def fullscrape(self, url):
    page = requests.get(url)
    soup = BeautifulSoup(page.content, 'lxml')
    content = soup.getText()
    print(content)

Но, похоже, это не работает должным образом.

Ответы [ 2 ]

0 голосов
/ 03 апреля 2020

Вам нужно сделать find_all('p'), выполнить итерацию и сохранить в виде списка.

content = [item.text для элемента в soup.find_all ('p')]

Код :

def fullscrape(self, url):
    page = requests.get(url)
    soup = BeautifulSoup(page.content, 'lxml')
    content =[item.text for item in soup.find_all('p')]
    print(content)
0 голосов
/ 03 апреля 2020

Попробуйте:

for c in content:
   print(c)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...