Python 正在尝试提取特定div和sub div下的数据

Python 正在尝试提取特定div和sub div下的数据,python,beautifulsoup,Python,Beautifulsoup,我正试图得到它,这样我就可以让它打印书名和章节,但只打印每本书和标题 所以基本上 “雅各布的第一本书” 第1-7章 而不是反复阅读所有的书 这是页面布局(python代码中包含的url) 这是输出 Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Chapte

我正试图得到它,这样我就可以让它打印书名和章节,但只打印每本书和标题

所以基本上 “雅各布的第一本书” 第1-7章

而不是反复阅读所有的书

这是页面布局(python代码中包含的url)

这是输出

Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 10
Chapter 11
Chapter 12
Chapter 13
Chapter 14
Chapter 15
Chapter 16
Chapter 17
Chapter 18
Chapter 19
Chapter 20
Chapter 21
Chapter 22
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 10
Chapter 11
Chapter 12
Chapter 13
Chapter 14
Chapter 15
Chapter 16
Chapter 17
Chapter 18
Chapter 19
Chapter 20
Chapter 21
Chapter 22
Chapter 23
Chapter 24
Chapter 25
Chapter 26
Chapter 27
Chapter 28
Chapter 29
Chapter 30
Chapter 31
Chapter 32
Chapter 33
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 1
Chapter 1

只要去掉数组中的最后2个,控件就没有那么细粒度了,因为html标记中没有任何id或名称

links = soup.select('dl dd dt')
for item in links[:-2]:
    title = str(item.get_text()).split(' ', 1)[1]
    print title

假设知道它们始终是第一个和第二个值,则可以使用数组引用:

title = links[0];
subtitle = links[1];

你可以试试这样的。首先,找到一本书名为《雅各书》的书:

然后选择书名直接同级的
,并在该
元素中查找所有相应的章节:

links = book.parent.select('+ dd > dl > dt')
for item in links:
    title = str(item.get_text()).split(' ', 1)[1]
    print title
输出:

The Book of Jacob
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7

我更新了我的问题。我需要能够选择第一个,然后是第二个,或第三个,因为这是一个有很多部分的页面。这似乎不适合我需要的工作。我更新了问题以澄清。该网站有不止一个标题,持续一段时间。我需要能够更改条款并选择,比如标题1或标题7等。您需要什么的描述一点也不清楚-存在一些矛盾。发布预期输出-可能打印结果-将帮助我们了解您实际尝试的内容achieve@har07谢谢你,我继续澄清了这个问题,并把输出放在了一起,试图更好地解释它。
book_title = 'The Book of Jacob'
book = soup.find('a', text=book_title)
print book.text
links = book.parent.select('+ dd > dl > dt')
for item in links:
    title = str(item.get_text()).split(' ', 1)[1]
    print title
The Book of Jacob
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7