无法选择某个元素

时间:2017-07-28 07:13:28

标签: python css-selectors lxml

我已经编写了一个css选择器来从网页中选择一个特定的元素。在执行时我可以看到我的选择器能够找到“类型:”但我希望它的值在这种情况下是“戏剧”。我怎么能这样做?

我试过了:

.mdif li b

项目所在的元素:

<div class="mdif">
<ul>
    <li><b>Genre:</b>Drama</li>
    <li><b>Quality:</b>1080p</li>
    <li><b>Screen:</b>1920x1080</li>
    <li><b>Size:</b>1.67G</li>
    <li><b>Rating:</b>6.1/10</li>
    <li><b>Peers:</b>17</li>
    <li><b>Seeds:</b>0</li>
</ul>
<a href="/movie/55000/download-lake-mungo-2008-1080p-mp4-yify-torrent.html" class="small button orange" target="_blank" title="Download Lake Mungo (2008) 1080p YIFY Torrent">Download</a>

6 个答案:

答案 0 :(得分:1)

您可以将所需的样式应用于.mdif li,并为.mdif li b

重置此样式

ul {
  list-style: none;
}
ul li {
  color: #ccc;
}

ul li b {
  color: #000;
}
<ul>
  <li><b>Title: </b>some text 1</li>
  <li><b>Title: </b>some text 2</li>
  <li><b>Title: </b>some text 3</li>
  <li><b>Title: </b>some text 4</li>
</ul>

答案 1 :(得分:1)

如果我能够正确理解您的问题,请使用

var text_val = $('.mdif').children('Genre').text();

将帮助您选择值,考虑到每个人都有一个id,例如:

HTML:

<div id='first'>
    <div id='Genre'>Drama</div>
    <div id='Quality:'>1080p</div>
    <div id='Screen:'>1920x1080</div>
    <div id="Size:">1.67G</div>

</div>

CSS:

var text_val = $('.mdif').children('div').text();

您可以在此处查看jsfiddle

如果您正在寻找其他产品,请说明。谢谢。

答案 2 :(得分:0)

使用此:

.mdif li:first-child { 
  color: red;
} 

.mdif li:first-child b{
  color: #000;
} 

&#13;
&#13;
li {
  list-style-type: none;
  position: relative;
} 

li:before {
  content: '';
  width: 5px;
  height: 5px;
  background-color: #000;
  border-radius: 100%;
  position: absolute;
  left: -15px;
  top: 8px;

}

.mdif li:first-child { 
  color: red;
} 

.mdif li:first-child b {
  color: #000;
}
&#13;
<div class="mdif">
   <ul>
    <li><b>Genre:</b><span>Drama</span></li>
    <li><b>Quality:</b><span>1080p</span></li>
    <li><b>Screen:</b><span>1920x1080</span></li>
    <li><b>Size:</b><span>1.67G</span></li>
    <li><b>Rating:</b><span>6.1/10</span></li>
    <li><b>Peers:</b><span>17</span></li>
    <li><b>Seeds:</b><span>0</span></li>
    </ul>
    <a href="/movie/55000/download-lake-mungo-2008-1080p-mp4-yify-torrent.html" class="small button orange" target="_blank" title="Download Lake Mungo (2008) 1080p YIFY Torrent">Download</a>
</div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

HTML

<div class="mdif">
  <ul>
    <li><b>Genre:</b><span>Drama</span></li>
    <li><b>Quality:</b><span>1080p</span></li>
    <li><b>Screen:</b><span>1920x1080</span></li>
  </ul>
</div>

CSS选择器

.mdif li span

答案 4 :(得分:0)

看到这个我选择了戏剧。

&#13;
&#13;
.mdif > ul > li > i{
	color:red;
}
&#13;
<div class="mdif"><ul><li><b>Genre:</b><i class="one">Drama</i></li> <li><b>Quality:</b>1080p</li> <li><b>Screen:</b>1920x1080</li> <li><b>Size:</b>1.67G</li> <li><b>Rating:</b>6.1/10</li> <li><b>Peers:</b>17</li> <li><b>Seeds:</b>0</li> </ul><a href="/movie/55000/download-lake-mungo-2008-1080p-mp4-yify-torrent.html" class="small button orange" target="_blank" title="Download Lake Mungo (2008) 1080p YIFY Torrent">Download</a></div>
&#13;
&#13;
&#13;

答案 5 :(得分:0)

终于找到了解决方案。这是完整的代码。

以上帖子中粘贴的Html元素:

html = '''
<div class="mdif">
<ul>
    <li><b>Genre:</b>Drama</li>
    <li><b>Quality:</b>1080p</li>
    <li><b>Screen:</b>1920x1080</li>
    <li><b>Size:</b>1.67G</li>
    <li><b>Rating:</b>6.1/10</li>
    <li><b>Peers:</b>17</li>
    <li><b>Seeds:</b>0</li>
</ul>
</div>
'''

剧本:

from lxml.html import fromstring

root = fromstring(html)
for items in root.cssselect(".mdif li"):
    item = items.cssselect("b")[0].tail.strip()
    print(item)

结果:

Drama
1080p
1920x1080
1.67G
6.1/10
17
0