Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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 2.7 如何在python中通过将变量与标记值匹配来提取数据_Python 2.7 - Fatal编程技术网

Python 2.7 如何在python中通过将变量与标记值匹配来提取数据

Python 2.7 如何在python中通过将变量与标记值匹配来提取数据,python-2.7,Python 2.7,--这是第一个表,从中我得到4个id(abc1--abc4),我需要与下表匹配,并获得所需的数据--! 1。 第一个话题 !--这是第一位演讲者和他/她的文本出现的地方---! “第一位发言者” “一些文本” !--这就是第二个演讲者进来的地方---! “第二个扬声器” “一些文本” !--然后是另一个id的行--! 2。 第二个话题 !--就像以前一样,这也将有一组发言者,他们有一些文本--! 我有两个具有相同类名的表,即BigClass。从第一个表中,我提取了4个ID,分别是a

--这是第一个表,从中我得到4个id(abc1--abc4),我需要与下表匹配,并获得所需的数据--!
1。
第一个话题

!--这是第一位演讲者和他/她的文本出现的地方---!
“第一位发言者”

“一些文本” !--这就是第二个演讲者进来的地方---! “第二个扬声器”
“一些文本”

!--然后是另一个id的行--! 2。 第二个话题 !--就像以前一样,这也将有一组发言者,他们有一些文本--!
我有两个具有相同类名的表,即BigClass。从第一个表中,我提取了4个ID,分别是abc1、abc2、abc3、abc4。 现在我想检查这些ID是否存在于第二个表中(它是) 在它与第二个表中的ID匹配之后,我想提取说话人和这些说话人的文本。
您可以看到我要提取数据的第二个表rom的代码结构。

提取说话人和文本信息的最佳方法似乎是提取列表中的所有ID和另一个列表中的所有说话人信息。然后只需交叉引用所需的ID并获得相应的说话人信息

我在这里创建了一个字典,其中键作为ID,值作为说话人信息。我通过td字段在包含speaker info的所有字段中定义了style属性的条件找到了speaker info

为了从HTML中提取信息,我使用了这个库

!--This is the first table from where i get 4 id's (abc1---abc4) which i need to match with the table below and get the required data--!
<table width="100%" border="0" class=""BigClass">
<tbody>..</tbody>
</table>

!--This is the second table --!
<table width="100%" border="0" class=""BigClass">
<tbody>
<tr align="left">
<td valign="top" colspan="2">
<strong>   1.              
             First Topic
</strong>
<a name="abc1" id="abc1"></a>
</td>
</tr>
!--This is the place where the first speaker and his/her text comes---!
<tr align="left">
<td style="text-align:justify;line-height:2;padding-right:10px;" colspan="2">
<strong> "   First Speaker    "    </strong>
<br>
"    Some Text   "
</td>
</tr>

!--This is where the second speaker comes in---!
<tr align="left">
<td style="text-align:justify;line-height:2;padding-right:10px;" colspan="2">
<strong>  "  Second Speaker    "   </strong>
<br>
     "   Some Text   "    
</td>
</tr>

<tr><td colspan="2"><br></td></tr>
<tr><td colspan="2"><br></td></tr>

!--Then here comes the row with another id--!
<tr align="left">
<td valign="top" colspan="2">
<strong>   2.              
             Second Topic
</strong>
<a name="abc2" id="abc2"></a>
</td>
</tr>
!--Just like before, this will also have set of speakers who have some text--!
这将为我提供以下输出:

from bs4 import BeautifulSoup
from itertools import izip

soup = BeautifulSoup(open('table.html'))
idList = []
speakerList = []
idsRequired = ['abc1','abc2']
for a in soup.findAll('a'):
    if 'id' in a.attrs.keys():
        idList.append(a.attrs['id'])

for i in soup.findAll('td'):
    if 'style' in i.attrs.keys():
        speakerList.append(i.text)

for key,value in izip(idList,speakerList):
    if key in idsRequired:
        print value 

请提供整个HTML文件并正确格式化。Thanks@Nitin请回答。html看起来像是原始文章中的完整代码above@Nitin-我尝试了你在下面提到的代码。但它第一次显示keyrerror:'id'。我想知道问题出在哪里。你能告诉我为什么我要面对这个错误吗?。但是谢谢你的代码,给了我一个更好的逻辑。@Nitin-@Nitin是的,这个是正确的,但不是我想要的。我的想法是只为id abc1而不是abc2打印这两个扬声器。id为abc时,它将在到达abc3之前打印其下方的扬声器,以此类推。希望您能进一步理解我的问题。@user2657822当您的标记中没有“id”属性时,会显示KeyError。如果您的HTML代码中的每个标记都有对应的ID,并且相应的标记具有说话人信息,则此代码将起作用。我已编辑代码以检查id密钥是否存在。您只需更改idsRequired列表即可获得所需的id。要了解BeautifulSoup的工作原理,可能需要仔细阅读他们的文档。@Nitin错误不再存在,但我认为id验证在这里不起作用。如果我将idsRequired=['abc1'],它应该同时显示firstspeaker和secondspeaker,但它不显示。我正在努力,但做不出来。
"     First speaker   "
"   Some text     "


"     Second speaker   "
"   Some text     "