使用Beautiful Soup select或lxml xpath从html获取href

2024-05-18 23:30:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在烂西红柿网站上做一些网页抓取,为了example here。你知道吗

我将Python与漂亮的Soup和lxml模块一起使用。你知道吗

我想提取电影信息,例如: -类型:戏剧、音乐和表演艺术

  • 导演:Kirill Serebrennikov

  • 作者:米哈伊尔·伊多夫、莉莉·伊多娃、伊万·卡皮托诺夫、基里尔·塞雷布伦尼科夫、纳塔利亚·诺门科

  • 作者(链接):名人/迈克尔•伊多夫,/名人/莉莉•伊多娃,/名人/伊万•卡皮托诺夫,/名人/基里尔•塞雷布伦尼科夫,/名人/娜塔莉娅•诺曼科

我检查了html页面以获取路径指南:

                    <li class="meta-row clearfix">
                        <div class="meta-label subtle">Rating: </div>
                        <div class="meta-value">NR</div>
                    </li>


                    <li class="meta-row clearfix">
                        <div class="meta-label subtle">Genre: </div>
                        <div class="meta-value">

                                <a href="/browse/opening/?genres=9">Drama</a>, 

                                <a href="/browse/opening/?genres=12">Musical &amp; Performing Arts</a>

                        </div>
                    </li>


                    <li class="meta-row clearfix">
                        <div class="meta-label subtle">Directed By: </div>
                        <div class="meta-value">

                                <a href="/celebrity/kirill_serebrennikov">Kirill Serebrennikov</a>

                        </div>
                    </li>


                    <li class="meta-row clearfix">
                        <div class="meta-label subtle">Written By: </div>
                        <div class="meta-value">

                                <a href="/celebrity/michael_idov">Mikhail Idov</a>, 

                                <a href="/celebrity/lily_idova">Lili Idova</a>, 

                                <a href="/celebrity/ivan_kapitonov">Ivan Kapitonov</a>, 

                                <a href="/celebrity/kirill_serebrennikov">Kirill Serebrennikov</a>, 

                                <a href="/celebrity/natalya_naumenko">Natalya Naumenko</a>

                        </div>
                    </li>


                    <li class="meta-row clearfix">
                        <div class="meta-label subtle">In Theaters: </div>
                        <div class="meta-value">
                            <time datetime="2019-06-06T17:00:00-07:00">Jun 7, 2019</time>
                            <span style="text-transform:capitalize">&nbsp;limited</span>
                        </div>
                    </li>




                    <li class="meta-row clearfix">
                        <div class="meta-label subtle">Runtime: </div>
                        <div class="meta-value">
                            <time datetime="P126M">
                                126 minutes
                            </time>
                        </div>
                    </li>


                    <li class="meta-row clearfix">
                    <div class="meta-label subtle">Studio: </div>
                    <div class="meta-value">

                            <a href="http://sonypictures.ru/leto/" target="movie-studio">Gunpowder &amp; Sky</a>

                    </div>

            </li>

我创建了如下html对象:

    page_response = requests.get(url, timeout=5)
    page_content = BeautifulSoup(page_response.content, "html.parser")
    tree = html.fromstring(page_response.content)

例如,对于作者来说,由于我只需要元素上的文本,因此很容易获得:

page_content.select('div.meta-value')[3].getText()

或使用xpart进行评级:

tree.xpath('//div[@class="meta-value"]/text()')[0]

对于所需的Writer链接(我遇到问题的地方),要访问html块,我执行以下操作:

page_content.select('div.meta-value')[3]

它给出:

<div class="meta-value">
<a href="/celebrity/michael_idov">Mikhail Idov</a>, 

                                <a href="/celebrity/lily_idova">Lili Idova</a>, 

                                <a href="/celebrity/ivan_kapitonov">Ivan Kapitonov</a>, 

                                <a href="/celebrity/kirill_serebrennikov">Kirill Serebrennikov</a>, 

                                <a href="/celebrity/natalya_naumenko">Natalya Naumenko</a>

或:

tree.xpath('//div[@class="meta-value"]')[3]

给予:

<Element div at 0x2915a4c54a8>

问题是我无法提取'href'。我想要的输出是:

/celebrity/michael_idov, /celebrity/lily_idova, /celebrity/ivan_kapitonov, /celebrity/kirill_serebrennikov, /celebrity/natalya_naumenko

我试过:

page_content.select('div.meta-value')[3].get('href')
tree.xpath('//div[@class="meta-value"]')[3].get('href')
tree.xpath('//div[@class="meta-value"]/@href')[3]

所有结果都为空或错误。 有人能帮我吗?你知道吗

提前谢谢! 干杯!你知道吗


Tags: divvaluehtmlpagelicontent名人label
1条回答
网友
1楼 · 发布于 2024-05-18 23:30:08

尝试以下脚本以获取您感兴趣的内容。一定要用不同的电影来测试它们。我想他们都能生产出所需的产品。我试图避免任何硬编码索引以内容为目标。你知道吗

使用css选择器:

import requests
from bs4 import BeautifulSoup

r = requests.get('https://www.rottentomatoes.com/m/leto')
soup = BeautifulSoup(r.text,'lxml')

directed = soup.select_one(".meta-row:contains('Directed By') > .meta-value > a").text
written = [item.text for item in soup.select(".meta-row:contains('Written By') > .meta-value > a")]
written_links = [item.get("href") for item in soup.select(".meta-row:contains('Written By') > .meta-value > a")]
print(directed,written,written_links)

使用xpath:

import requests
from lxml.html import fromstring

r = requests.get('https://www.rottentomatoes.com/m/leto')
root = fromstring(r.text)

directed = root.xpath("//*[contains(.,'Directed By')]/parent::*/*[@class='meta-value']/a/text()")
written = root.xpath("//*[contains(.,'Written By')]/parent::*/*[@class='meta-value']/a/text()")
written_links = root.xpath(".//*[contains(.,'Written By')]/parent::*/*[@class='meta-value']/a//@href")
print(directed,written,written_links)

在cast的情况下,我使用了列表理解,这样我就可以在单个元素上使用.strip()来清除空白。normalize-space()是最理想的选择。你知道吗

cast = [item.strip() for item in root.xpath("//*[contains(@class,'cast-item')]//a/span[@title]/text()")]

相关问题 更多 >

    热门问题