【问题标题】:Selecting part of html after scraping抓取后选择部分html
【发布时间】:2019-06-04 18:34:03
【问题描述】:

我已经用一个包含以下内容的 html 键值抓取了一个 json 文件:

"<div class='car_model_estimation_result__container'>\n<div class='car_model_estimation_result cobalt-mb-tight'>\n<div class='car_model_estimation_result__item'>\n<span class=\"car_model_estimation_result_amount\">720€</span>\n<p class='cobalt-text-sectionHeader'>\n<span>maximum estimés par mois</span>\n<span class='cobalt-mb-unit cobalt-Icon cobalt-Icon--size16 cobalt-Icon--colorGraphiteLight'>\n<a class=\"js_popup_trigger\" href=\"#estimate_about_with_open\"><svg viewBox=\"0 0 24 24\" xmlns=\"http://www.w3.org/2000/svg\">\n <path d=\"M11 9h2V7h-2v2zm1 11c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8zm0-18C6.477 2 2 6.477 2 12A10 10 0 1 0 12 2zm-1 15h2v-6h-2v6z\" />\n</svg>\n\n</a></span>\n</p>\n\n</div>\n<div class='owner_homepage_hero_estimation_cta__container'>\n<a class=\"owner_homepage_hero_estimation_cta--fullWidth cobalt-Button cobalt-Button--primary cobalt-Button--large js_rent_my_car js_rent_my_car_top js_estimation_result\" rel=\"nofollow\" data-tracking-params=\"{"model_id":"1519","brand_id":"67","mileage":4,"city":"Anvers","release_year":2016,"open_eligible":true,"currency":"EUR","earnings":720,"earnings_period":"month"}\" data-click-location=\"top\" data-estimated-earnings=\"{"model_id":"1519","release_year":2016,"mileage":4,"within_eligible_area":true,"open_eligible":true}\" href=\"/choose_open_or_standard?mileage=4&model_id=1519&open_eligible=true&release_year=2016&within_eligible_area=true\">Inscrire ma voiture</a>\n</div>\n</div>\n</div>\n"

我想获取类car_model_estimation_result_amountspan 属性的文本。有没有办法在不使用正则表达式的情况下做到这一点?

【问题讨论】:

  • 是的,使用像beautifulsoup这样的html解析器。你不应该使用正则表达式来提取一些 html 属性/标签。仅使用正则表达式提取非结构化文本中的信息。

标签: python scrapy css-selectors


【解决方案1】:

将文本加载到ParselSelector 中并像使用response 一样使用它来查找所需的数据:

from parsel import Selector
selector = Selector(json_value['html'])
desired_text = selector.css('span.car_model_estimation_result_amount').get()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    • 1970-01-01
    • 2011-04-23
    • 2017-03-22
    • 2021-01-30
    相关资源
    最近更新 更多