![](/img/trans.png)
[英]Accessing an invisible element in html source code using beautiful soup python
[英]How to scrape a single element out of 2 elements having same set of attributes and same hierarchy in html source code (using python's beautiful soup)
一种方法可能是迭代<p class="sort-num_votes-visible">
所有兄弟,如果你发现一个<span name="nv">
被<span class="text-muted">
包围了<span class="ghost">
那么这必须是您正在寻找的范围。 这当然意味着HTML片段的结构始终是相同的。 如果其中一个span
s可能丢失,那么这种方法显然会失败。
如果保证这两个跨度总是在那里并且按照确切的顺序你可以做这样的事情(你的html_soup
HTML在html_soup
):
votes = html_soup.find("p", {"class": "sort-num_votes-visible").find_all("span", {"name": "nv"})[0]
编辑:
根据您的评论,您可以执行以下操作以解析多部电影的投票:
for p in html_soup.find("p", {"class": "sort-num_votes-visible"}):
votes = p.find_all("span", {"name": "nv"})[0]
< Put whatever code here for each of your movies
...
>
你可以使用这样的东西(假设你使用的是BeautifulSoup):
soup = BeautifulSoup('yourhtml', 'lxml')
p_sort = soup.find('p', {'class':'sort-num_votes-visible'})
req_span = p_sort.find_all('span', {'name':'nv'})[0]
req_span
将包含您询问的标记。
如果这两个相似的span元素的顺序相同,那么您可以选择result的第一个元素或使用.find()而不是.find_all()[0]。
我认为parsel
是一个更好的html解析包,支持xpath
。
from parsel import Selector
s = Selector(text=html)
nv_data = s.xpath('//span[@name="nv"]/@data-value').extract_first()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.