Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中提取具有相同html条件的多个元素_Python_Regex_Web Scraping_Beautifulsoup - Fatal编程技术网

在python中提取具有相同html条件的多个元素

在python中提取具有相同html条件的多个元素,python,regex,web-scraping,beautifulsoup,Python,Regex,Web Scraping,Beautifulsoup,我是python新手,而且很漂亮,所以这个答案可能是显而易见的 我正在使用BeautifulSoup解析以下html并提取日期 html=''' <p><strong>Event:</strong>Meeting</p> <p><strong>Date:</strong> Mon, Apr 25, 2016, 11 am</p> <p><strong>Price:</st

我是python新手,而且很漂亮,所以这个答案可能是显而易见的

我正在使用BeautifulSoup解析以下html并提取日期

html='''
<p><strong>Event:</strong>Meeting</p>
<p><strong>Date:</strong> Mon, Apr 25, 2016, 11 am</p>
<p><strong>Price:</strong>$20.00</p>

<p><strong>Event:</strong>Convention</p>
<p><strong>Date:</strong> Mon, May 2, 2016, 11 am</p>
<p><strong>Price:</strong>$25.00</p>

<p><strong>Event:</strong>Dance</p>
<p><strong>Date:</strong> Mon, May 9, 2016, 11 am</p>
<p><strong>Price:</strong>Free</p>
'''
在bs4中有没有办法做到这一点,或者我应该使用正则表达式。还有其他建议吗

所需列表输出:


['Mon,Apr 25,2016,11 am','Mon,May 2,2016,11 am','Mon,May 9,2016,11 am']

我可能会迭代找到的每个元素并将其附加到列表中。类似这样的东西可能(未经测试):


我可能会迭代找到的每个元素,并将其附加到列表中。类似这样的东西可能(未经测试):


新手错误…修复了它:

for x in range(0,len(date_raw)):
    date_add = date_raw[x].next_sibling.strip()
    date_list.append(date_add)
    print (date_add)

新手错误…修复了它:

for x in range(0,len(date_raw)):
    date_add = date_raw[x].next_sibling.strip()
    date_list.append(date_add)
    print (date_add)

谢谢你的帮助。我张贴了答案。我必须输入索引并对其进行迭代。感谢您的帮助。我张贴了答案。我必须输入索引并迭代。很好,你发现了你的错误。仅供参考,在Python中迭代
范围(len(something))
被认为是一种反模式。Ed Dunn在date_raw中用
回答d更为惯用。感谢您的反馈!你能详细说明一下这是一种反模式吗?我认为这是多余的,但在逻辑上并没有适得其反。这是一个反模式,因为它需要额外的不必要的步骤吗?特雷,我重新编写了代码。这看起来不太对劲,但这是我让它工作的唯一方法对于x-in-date\u-raw:date\u-add=date\u-raw[date\u-raw.index(x)]。下一个兄弟姐妹条带()日期列表。追加(date\u-add)
对于x-in-date\u-raw:date\u-add=date\u-raw[date\u-raw.index(x)]。下一个兄弟姐妹条带()日期列表。追加(date\u-add)
我相信
date\u-raw[date-index(x)]
可以是
x
。很好,您发现了错误。仅供参考,在Python中迭代
范围(len(something))
被认为是一种反模式。Ed Dunn在date_raw中用
回答d更为惯用。感谢您的反馈!你能详细说明一下这是一种反模式吗?我认为这是多余的,但在逻辑上并没有适得其反。这是一个反模式,因为它需要额外的不必要的步骤吗?特雷,我重新编写了代码。这看起来不太对劲,但这是我让它工作的唯一方法对于x-in-date\u raw:date\u add=date\u raw[date\u raw.index(x)]。next\u sibling.strip()date\u list.append(date\u add)
对于x-in-date\u raw:date\u add=date\u raw[date\u raw.index(x)]。next\u sibling.strip()date\u list.append(date\u add)
我相信
可以是
x
for x in range(0,len(date_raw)):
    date_add = date_raw[x].next_sibling.strip()
    date_list.append(date_add)
    print (date_add)