簡體   English   中英

模式Web無法通過類名稱找到元素

[英]Pattern web unable to locate elements by class names

我正在嘗試通過類名來標識DOM元素,但是我無法使用docs中所述的pattern.web(我也在運行我以前使用的代碼,因此它確實在某些時候起作用了)。

from pattern.web import DOM

html = """<html><head><title>pattern.web | CLiPS</title></head>
<body>
  <div class="class1 class2 class3">
    <form action="/pages/pattern-web"  accept-charset="UTF-8" method="post" id="search-block-form">
      <div>
        <label for="edit-search-block-form-1">Search this site: </label>
      </div>
    </form>
  </div>
</body></html>"""

dom = DOM(html)
print "Search Results by Method:"
print 'tag[attr="value"] Notation Results:'
print dom('div[class="class1 class2 class3"]')
print 
print 'tag.class Notation Results:'
print dom('div.class1')
print
print 'By class, no tag results:'
print dom.by_class('class1')
print 
print 'Looping through all divs and printing matching results:'
for i in dom('div'):
    if 'class' in i.attrs and i.attrs['class'] == 'class1 class2 class3':
        print i.attrs

注意( ElementDOM函數是可互換的,並且給出相同的結果)。 結果是這樣的:

Search Results by Method:
tag[attr="value"] Notation Results:
[]

tag.class Notation Results:
[]

By class, no tag results:
[Element(tag='div')]

Looping through all divs and printing matching results:
{u'class': u'class1 class2 class3'}

如您所見,使用tag.class表示法和tag[attr="value"]表示法by_class結果都是空的,但是by_class返回一個結果。 顯然,具有那些屬性的元素存在。 如何搜索具有所有3個類的所有div?

過去,我已經能夠使用dom('div.class1.class2.class3')進行搜索,以識別所有3個類的div。 這不僅不起作用,而且還給我unicode錯誤(似乎第二個句點導致unicode錯誤): TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode'

問題 :過去,我已經能夠使用dom('div.class1.class2.class3')進行搜索,以識別所有3個類的div。


閱讀源代碼github.com/clips/pattern/blob/master/pattern/web
發現,這只是使用Beautiful Soup的包裝。

#Beautiful Soup被包裝在DOM,Element和Text類中,類似於Javascript DOM。
#美麗湯也可以直接使用


這是一個已知問題,請參見SO: 漂亮的湯find_all找不到具有多個類的CSS選擇器

解決方法是使用.select(...)而不是.find_all(...)
pattern.web找不到.select(...)

例如:

from bs4 import BeautifulSoup

html = """<html><head><title>pattern.web | CLiPS</title></head>
  <body>
    <div class="class1 class4">
      <form action="/pages/pattern-web"  accept-charset="UTF-8" method="post" id="search-block-form">
        <div class="class1 class2 class3">
          <label for="edit-search-block-form-1">Search this site: </label>
        </div>
      </form>
    </div>
</body></html>
"""
soup = BeautifulSoup(html, 'html.parser')
div = soup.select('div.class1.class2')
print("{}".format(div))

輸出

 [<div class="class1 class2 class3"> <label for="edit-search-block-form-1">Search this site: </label> </div>] 

問題 :它也給我unicode錯誤(似乎第二個句點導致unicode錯誤):

 TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode' 

未知的是,此TypeError是來自pattern.web還是Beautiful Soup
根據這個SO: descriptor-join-requires-unicode-object-but-received-a-str,它是標准的Python消息。


使用來自GitHub的pattern.web ,結果符合預期:

from pattern.web import Element

elements = Element(html)
print("Search Results by Method:")
print('tag[attr="value"] Notation\tResults:{}'
    .format(elements('div[class="class1 class2 class3"]')))

print('tag.class Notation \t\t\tResults:{}'
    .format(elements('div.class1.class2.class3')))

print('By class, no tag \t\t\tResults:{}'
    .format(elements.by_class('class1 class2 class3')))

print('Looping through all divs and printing matching results:')
for i in elements('div'):
    if 'class' in i.attrs:
        if " ".join(i.attrs['class']) == 'class1 class2 class3':
            print("\tmatch:{}".format(i.attrs))

輸出

 Search Results by Method: tag[attr="value"] Notation Results:{'class': ['class1', 'class2', 'class3']} tag.class Notation Results:{'class': ['class1', 'class2', 'class3']} By class, no tag Results:{'class': ['class1', 'class2', 'class3']} Looping through all divs and printing matching results: match:{'class': ['class1', 'class2', 'class3']} 

使用Python:3.5.3-pattern.web:3.6-bs4:4.5.3測試

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM