2017-06-27 3 views
0
print("<&s &s>" &(element.tag,element.attrib)) 

はAttributeError: 'リスト' オブジェクト一切属性 'タグ''print_element'メソッドで何が間違っていますか?

from lxml import html 
import requests 

def print_element(element): 
    print("<&s &s>" &(element.tag,element.attrib)) 

r = requests.get("https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_and_their_capitals_in_native_languages") 

tree = html.fromstring(r.content) 
s = tree.xpath('//table') 
print_element(s) 

を持っていない任意の助けいただければ幸いです。ありがとう!

答えて

0

あなたが持っているのはリストなので、リスト内のアイテムを使用する必要があります。そして、 '&'を '%'に変更する必要があります。

from lxml import html 
import requests 

def print_element(element): 
    print("<%s %s>" %(element.tag,element.attrib)) 

r = requests.get("https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_and_their_capitals_in_native_languages") 

tree = html.fromstring(r.content) 
s = tree.xpath('//table') 
print type(s) # you will see this s is a list 
for item in s: 
    print_element(item) 
0

elementは「要素」のリストであり、単一の要素ではないようです。

関連する問題