python – 使用BeautifulSoup提取特定的dl和dd列表元素

我第一次发帖.我正在使用BeautifulSoup 4和python 2.7(pycharm).我有一个包含元素的网页,我需要提取标签为“Salary:”或“Date:”的特定元素,该页面包含多个列表.

问题:我似乎无法识别和提取特定文本.我搜索了这个网站并尝试没有成功.

示例html:

<dl><dt>Date:</dt><dd>13 September 2015</dd><dt>Salary:</dt><dd>Starting at £40,130 per annum.</dd></dl><dl><dt>Date:</dt><dd>15 December 2015</dd><dt>Salary:</dt><dd>Starting at £22,460 per annum.</dd></dl><dl><dt>Date:</dt><dd>10 January 2014</dd><dt>Salary:</dt><dd>Starting at £18,160 per annum.</dd></dl>

我尝试过的代码没有成功:

r = requests.get("http://www.mywebsite.com/test.html")
soup = BeautifulSoup(r.content, "html.parser")
dl_data = soup.find_all("dl")
for dlitem in dl_data: 
    print dlitem.find("dt",text="Date:").parent.findNext("dd").contents[0]
    print dlitem.find("dt",text="Salary:").parent.findNext("dd").contents[0]

预期结果:

13 September 2015
15 December 2015
10 January 2014
Starting at £40,130 per annum.
Starting at £22,460 per annum.
Starting at £18,160 per annum.

实际结果:

print dlitem.find("dt",text="Date:").parent.findNext("dd").contents[0]
AttributeError: 'NoneType' object has no attribute 'parent'

我已经尝试了这个代码的多种变体并绕圈了,我想出了如何将所有dd元素打印到屏幕上,而不是特定的dd元素!

谢谢

解决方法:

如果订单不重要,只需进行一些更改:

...
dl_data = soup.find_all("dd")
for dlitem in dl_data:
    print dlitem.string

结果:

13 September 2015
Starting at £40,130 per annum.
15 December 2015
Starting at £22,460 per annum.
10 January 2014
Starting at £18,160 per annum.

您的最新要求:

for item in list(zip(soup.find_all("dd")[0::3],soup.find_all("dd")[2::3])):
    date, salary = item
    print ', '.join([date.string, salary.string])

输出:

13 September 2015, 100
14 September 2015, 200
上一篇:如何在Python 2.7.3版本中提取7z zip文件


下一篇:使用Python从HTML页面中提取图像