使用BeautifulSoup选择多个元素并分别进行管理

时间:2019-02-19 16:17:55

标签: python web-scraping beautifulsoup html-parsing

我正在使用BeautifulSoup解析诗歌网页。诗歌分为h3(用于诗歌标题)和.line(用于诗歌的每一行)。我可以同时获得两个元素并将它们添加到列表中。但是我想将h3大写并指示换行符,然后将其插入行列表。

    linesArr = []
    for lines in full_text:
        booktitles = lines.select('h3')
        for booktitle in booktitles:
            linesArr.append(booktitle.text.upper())
            linesArr.append('')
        for line in lines.select('h3, .line'):
            linesArr.append(line.text)

此代码将所有书名附加到列表的开头,然后继续获取h3.line项目。我试过插入这样的代码:

    linesArr = []
    for lines in full_text:
        for line in lines.select('h3, .line'):
            if line.find('h3'):
                linesArr.append(line.text.upper())
                linesArr.append('')
            else:
                linesArr.append(line.text)

1 个答案:

答案 0 :(得分:0)

我不确定您要做什么,但是通过这种方式,您可以获取一个数组,其中标题为大写,且所有行都为

#!/usr/bin/python3
# coding: utf8

from bs4 import BeautifulSoup
import requests

page = requests.get("https://quod.lib.umich.edu/c/cme/CT/1:1?rgn=div2;view=fulltext")
soup = BeautifulSoup(page.text, 'html.parser')

title = soup.find('h3')
full_lines = soup.find_all('div',{'class':'line'})

linesArr = []
linesArr.append(title.get_text().upper())
for line in full_lines:
    linesArr.append(line.get_text())

# Print full array with the title and text
print(linesArr)

# Print text here with line break
for linea in linesArr:
    print(linea + '\n')