python - json.decoder.JSONDecodeError : Expecting value: line 1 column 1 (char 0) Scrapy

标签 python web-scraping beautifulsoup scrapy web-crawler

大家好,我正在尝试使用 scrapy/Beautifulsoup 废弃/抓取这个基于 json 的网站

https://pk.profdir.com/jobs-for-angular-developer-lahore-punjab-cddb

我编写了下面的代码来运行从网站读取/获取 json 的操作:

website_text = response.body.decode("utf-8")
jobs_soup = BeautifulSoup(website_text.replace("<", " <"), "html.parser")
script_tag = jobs_soup.find('script', {"type": 'application/ld+json'}).text
data = json.loads(script_tag, strict=False)

但是会一次又一次的出现这个错误:

raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

如果有人知道请帮助我,这对我很有帮助

最佳答案

位于 <script> 内的 json无效,因此 json默认情况下无法解码。 快速而肮脏的修复是替换 "description":通过re.sub (另外,使用 html5lib 作为 BeautifulSoup 解析器):

import re
import json
import requests
from bs4 import BeautifulSoup


url = "https://pk.profdir.com/jobs-for-angular-developer-lahore-punjab-cddb"
soup = BeautifulSoup(requests.get(url).content, "html5lib")

data = soup.select_one('script[type="application/ld+json"]').contents[0]

# fix "broken" description
data = re.sub(
    r'(?<="description" : )"(.*?)"(?=,\s+")',
    lambda g: json.dumps(g.group(1)),
    data,
    flags=re.S,
)

data = json.loads(data)

print(json.dumps(data, indent=4))

打印:

{
    "@context": "http://schema.org/",
    "@type": "JobPosting",
    "title": "angular-developer",
    "description": "<p>Designing and developing user interfaces using Angular best practices\n</p><p>\n</p><p>Adapting interface for modern internet applications using the latest front-end technologies\n</p><p>\n</p><p>Developing product analysis tasks and optimizing the user experience\n</p><p>\n</p><p>Proficiency in Angular, HTML, CSS, and JavaScript for rapid prototyping.\n</p><p>\n</p><p>Integration of APIs and RESTful Services.\n</p><p>\n</p><p>Creating Maintaining Mobile and Website Responsive Design and Mobile website.\n</p><p>\n</p><p>Developing Across Browsers\n</p><p>\n</p><p>Creating tools that improve site interaction regardless of the browser.\n</p><p>\n</p><p>Managing software workflow.\n</p><p>\n</p><p>Following SEO best practices Fixing bugs and testing for usability\n</p><p>\n</p><p>Conducting performance tests\n</p><p>\n</p><p>Consulting with the design team\n</p><p>\n</p><p>Ensuring high performance of applications and providing support\n</p><p>\n</p><p>\n</p><p>Job Requirements:\n</p><p>\n</p><p>\n</p><p>Expert knowledge of HTML5, CSS3\n</p><p>\n</p><p>Strong knowledge of JavaScript\n</p><p>\n</p><p>Experience in JS frameworks Angular\n</p><p>\n</p><p>Familiarity with Software version control systems e.g., Git\n</p><p>\n</p><p>Experience in Node.js\n</p><p>\n</p><p>Having knowledge of AWS environment is a plus\n</p><p>\n</p><p>AlienVault experience is a plus\n</p><p>\n</p><p>Jira Cloud experience is a plus\n</p><p>\n</p><p>Knowledge of CSS Pre-processor technologies including SASS\n</p><p>\n</p><p>Able to quickly transform visual designs into accurate HTML/CSS\n</p><p>\n</p><p>Ability to write high-performance, reusable code for UI components\n</p><p>\n</p><p>Strong understanding of security and performance fundamentals required\n</p><p>\n</p><p>Familiarity with the whole web stack, including protocols and web server optimization techniques\n</p><p>\n</p><p>Great communication skills You&#39;ll be interacting with Product and Development teams\n</p><p>\n</p><p>Experience in Grunt, Rollup, or Webpack is a plus\n</p><p>\n</p><p>Good Technical skills, Communication skills, General problem-solving skills, and Coding skills\n</p><p>\n</p><p>Package: Negotiable</p>",
    "identifier": {
        "@type": "PropertyValue",
        "name": "TTS",
        "value": "cddb"
    },
    "datePosted": "2022-02-18T00:00",
    "validThrough": "2022-05-19T00:00",
    "employmentType": "permanent<br>full time",
    "hiringOrganization": {
        "@type": "Organization",
        "name": "TTS",
        "sameAs": "https://pk.profdir.com/companies/tts-ebfu",
        "logo": "https://pk.profdir.com/apple-icon.png"
    },
    "jobLocation": {
        "@type": "Place",
        "address": {
            "@type": "PostalAddress",
            "streetAddress": "R Block DHA Phase 2",
            "addressLocality": "Lahore",
            "addressRegion": "Punjab",
            "postalCode": "53720",
            "addressCountry": "PK"
        }
    },
    "baseSalary": {
        "@type": "MonetaryAmount",
        "currency": "PKR",
        "value": {
            "@type": "QuantitativeValue",
            "value": "70000",
            "unitText": "MONTH"
        }
    }
}

关于python - json.decoder.JSONDecodeError : Expecting value: line 1 column 1 (char 0) Scrapy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71820282/

相关文章:

python 3 : remove a substring between two delimiting char

python - 将 Django 部署到 PythonAnywhere 并且无法使用环境变量中的 SECRET_KEY,尽管已设置

python - 使用Python自动化使用在线文件转换器

python:抓取站点时sslv3警报握手失败

python - 使用 Beautiful Soup 4 一次搜索多种标签

python - 在 Python 中拆分数组

python将dictreader写入csv文件

python - Web 抓取表可以正确读取错误数据

python - scrapy 项目加载器返回列表不是单个值

python - 网页抓取弹出窗口