未显示解析petshop.ru的结果

发布于 2025-01-26 02:33:26 字数 1158 浏览 3 评论 0 原文

我正在尝试分析页面,但它行不通。

    import requests
from bs4 import BeautifulSoup as BS

r = requests.get ("https://www.petshop.ru/catalog/cats/veterinary_feed/dlya_koshek_pri_zapore_fibre_response_fr31_5789/")
html = BS (r.content, 'html.parser')

for el in html.select (".style_product_head__ufClP > .style_tablet__bK5he style_desktop__3Zkvu"):
title = el.select ('.style_list__3V0_P > .style_price_wrapper__1HT8P')
print ( title[0].text )

我按照模型的方式做,因为不熟悉python:

import requests
from bs4 import BeautifulSoup as BS

r = requests.get ("https://stopgame.ru/review/new/izumitelno/p1")
html = BS (r.content, 'html.parser')

for el in html.select (".items > .article-summary "):
title = el.select ('.caption > a')
print ( title[0].text )

我希望看到以下结果:о儿цRTim

理想情况下,知道如何显示这种结果也很有趣: petshop.ru:„обыч音乐3 125₽,因为我计划实施更多网站的解析,以跟踪此猫食品的价格:)

I'm trying to parse the page https://www.petshop.ru/catalog/cats/veterinary_feed/dlya_koshek_pri_zapore_fibre_response_fr31_5789/, but it doesn't work.

    import requests
from bs4 import BeautifulSoup as BS

r = requests.get ("https://www.petshop.ru/catalog/cats/veterinary_feed/dlya_koshek_pri_zapore_fibre_response_fr31_5789/")
html = BS (r.content, 'html.parser')

for el in html.select (".style_product_head__ufClP > .style_tablet__bK5he style_desktop__3Zkvu"):
title = el.select ('.style_list__3V0_P > .style_price_wrapper__1HT8P')
print ( title[0].text )

I do according to the model, because unfamiliar with python:

import requests
from bs4 import BeautifulSoup as BS

r = requests.get ("https://stopgame.ru/review/new/izumitelno/p1")
html = BS (r.content, 'html.parser')

for el in html.select (".items > .article-summary "):
title = el.select ('.caption > a')
print ( title[0].text )

I expect to see the following result: Обычная цена

Ideally, it would also be interesting to know how to display a result of this kind: petshop.ru: Обычная цена 3 125 ₽ , because I plan to implement the parsing of several more sites to track prices for this cat food :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不可一世的女人 2025-02-02 02:33:26

您想要的所有数据都在HTML源中,并且它以JSON对象作为< script> tag的json对象出现。您可以轻松定位它并解析它。

如下:

import json
import re

import requests

url = "https://www.petshop.ru/catalog/cats/veterinary_feed/dlya_koshek_pri_zapore_fibre_response_fr31_5789/"

headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:99.0) Gecko/20100101 Firefox/99.0",
}

data = (
    json.loads(
        re.search(
            r'"onProductView",\s(.*)\);',
            requests.get(url, headers=headers).text
        ).group(1)
    )
)

print(f"{data['product']['name']} - {data['product']['price']} руб.")

输出:

Для кошек при запоре - 3125 руб.

All the data you want is in the HTML source and it comes as a JSON object in a <script> tag. You can easily target that and parse it.

Here's how:

import json
import re

import requests

url = "https://www.petshop.ru/catalog/cats/veterinary_feed/dlya_koshek_pri_zapore_fibre_response_fr31_5789/"

headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:99.0) Gecko/20100101 Firefox/99.0",
}

data = (
    json.loads(
        re.search(
            r'"onProductView",\s(.*)\);',
            requests.get(url, headers=headers).text
        ).group(1)
    )
)

print(f"{data['product']['name']} - {data['product']['price']} руб.")

Output:

Для кошек при запоре - 3125 руб.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文