2017-08-17 8 views
2

子供<orgname>を親の<assignee>から探して保存しようとしています。私のコードは、これまでのところ、すでに一定の他のタグを拾ってXMLドキュメントを通る - 私はそのように設定している:美味しいスープに根の子供を見つけて保存する

for xml_string in separated_xml(infile): # Calls the output of the separated and read file to parse the data 
    soup = BeautifulSoup(xml_string, "lxml")  # BeautifulSoup parses the data strings where the XML is converted to Unicode 
    pub_ref = soup.findAll("publication-reference") # Beginning parsing at every instance of a publication 

    lst = [] # Creating empty list to append into 

    with open('./output.csv', 'ab') as f: 
     writer = csv.writer(f, dialect = 'excel') 

     for info in pub_ref: # Looping over all instances of publication 

# The final loop finds every instance of invention name, patent number, date, and country to print and append 

      for inv_name, pat_num, date_num, country, city, state in zip(soup.findAll("invention-title"), soup.findAll("doc-number"), assign.find("orgname"), soup.findAll("date"), soup.findAll("country"), soup.findAll("city"), soup.findAll("state")): 

       writer.writerow([inv_name.text, pat_num.text, org_name.text, date_num.text, country.text, city.text, state.text]) 

を私はすでに、各発明の名称および特許のペアように順序でこれを持っており、組織の担当者を必要としますそれと一緒に名前。

<agent sequence="01" rep-type="attorney"> 
<addressbook> 
<orgname>Sawyer Law Group LLP</orgname> 
<address> 
<country>unknown</country> 
</address> 
</addressbook> 
</agent> 
</agents> 
</parties> 
<assignees> 
<assignee> 
<addressbook> 
<orgname>International Business Machines Corporation</orgname> 
<role>02</role> 
<address> 
<city>Armonk</city> 
<state>NY</state> 
<country>US</country> 
</address> 
</addressbook> 
</assignee> 
</assignees> 

私だけ<assignee>タグの下ORGNAMEをしたい:問題はこのように見ている弁護士や、そのような団体のようなものに関連付けられた他のタグがあるということです。私が試した:

アサイン= soup.findAll( "譲受人") ORG_NAME = assign.findAll( "ORGNAMEを")

しかし無駄に。

"ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?" % key

AttributeError: ResultSet object has no attribute 'find'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?

これらのタグを追加して、譲受人タグの下にあるすべてのorgnameを見つけるにはどうすればよいですか? シンプルなようですが、私はそれを得ることができません。

ありがとうございます。

答えて

3

assign = soup.findAll("assignee")は、リストを返すことがorg_name = assign.findAll("orgname")が失敗した呼び出している理由ので、あなたはassignの各要素を通過し、それが.findAll("orgname")だ呼び出す必要があるだろうが、一つだけ<orgname><assignee>ではありますようだ、そうする必要はありません.findの代わりに.findAllを使用してください。リストの内包表記を使用してassignの各要素に.findを使用してみてください:

orgnames = [item.find("orgname") for item in assign] 

または、直接そのテキストを取得するには、<orgname>はその<assignee>内に存在する場合は前にチェック:

orgnames = [item.find("orgname").text for item in assign if item.find("orgname")] 
+0

これは動作するはずです、ヴィニシウス・アギアル@ 。唯一のことは、私が追加した後、NoneTypeオブジェクトに属性 'text'メッセージが表示されていないことです。 'assign = soup.findAll(assignee") '--->' item in assign: ' - > 'org = item.find(" orgname ")'最後に 'org.text'をwriterowsの出力に追加します。何かご意見は? – HelloToEarth

+0

ああ、そうです、それは私の解決策の問題です。もしあなたが 'org.text'の前に' if org: 'を追加する必要があります、私は答えを編集します。 @HelloToEarthはちょうど編集しました、それは今働くべきです! –

+0

私はつかまえました。そこで起こったことは、解析したときにリストからテキストとして引き出すことを条件としない限り、タグ自体をリスト自体として解析したことです。 ありがとうございました。 – HelloToEarth

関連する問題