【问题标题】:Scrapy + Python, Error in Finding links from a websiteScrapy + Python,从网站查找链接时出错
【发布时间】:2019-02-22 05:34:18
【问题描述】:

我正在尝试查找此页面所有事件的 URL:

https://www.eventshigh.com/delhi/food?src=exp

但我只能看到 JSON 格式的 URL:

 {
    "@context":"http://schema.org",
    "@type":"Event",
    "name":"DANDIYA NIGHT 2018",
    "image":"https://storage.googleapis.com/ehimages/2018/9/4/img_b719545523ac467c4ad206c3a6e76b65_1536053337882_resized_1000.jpg",
    "url":"https://www.eventshigh.com/detail/Delhi/5b30d4b8462a552a5ce4a5ebcbefcf47-dandiya-night-2018",
    "eventStatus": "EventScheduled",

    "startDate":"2018-10-14T18:30:00+05:30",
    "doorTime":"2018-10-14T18:30:00+05:30",

      "endDate":"2018-10-14T22:30:00+05:30",

    "description" : "Dress code : TRADITIONAL (mandatory)\u00A0 \r\n Dandiya sticks will be available at the venue ( paid)\u00A0 \r\n Lip smacking food, professional dandiya Dj , media coverage , lucky draw \u00A0, Dandiya Garba Raas , Shopping and Games .\u00A0 \r\n \u00A0 \r\n Winners\u00A0 \r\n \u00A0 \r\n Best dress ( all",
    "location":{
      "@type":"Place",


          "name":"K And L Community Hall (senior Citizen Complex )",


          "address":"80 TO 49, Pocket K, Sarita Vihar, New Delhi, Delhi 110076, India"



    },

这里是:

"url":"https://www.eventshigh.com/detail/Delhi/5b30d4b8462a552a5ce4a5ebcbefcf47-dandiya-night-2018"

但我找不到任何其他包含链接的 HTML/XML 标记。我也找不到包含链接的相应 JSON 文件。你能帮我把这个页面所有事件的链接刮下来吗:

https://www.eventshigh.com/delhi/food?src=exp

【问题讨论】:

标签: python json scrapy web-crawler


【解决方案1】:

从 JavaScript 驱动的页面(例如这个页面)收集信息一开始可能会让人望而生畏;但实际上通常效率更高,因为所有信息都在一个地方,而不是分散在大量昂贵的 HTTP 请求查找中。

因此,当页面为您提供这样的 JSON 数据时,您可以通过对服务器友好来感谢他们并使用它! :)
在您已经收集到的“源视图分析”上投入一点时间,这也将比尝试通过(昂贵的)Selenium/Splash/ect.-renderpipe 获取信息更有效。

实现这一目标的无价工具是 XPath。有时可能需要我们的朋友regex 提供一些额外的帮助。
假设您已成功获取该页面,并且有一个 Scrapy response 对象(或者您在其他收集的响应正文上有一个 Parsel.Selector()),您将能够以 response.xpath 或 @ 访问 xpath() 方法987654327@:

>>> response.status
200

您已确定数据以纯文本 (json) 形式存在,因此我们需要深入了解它的隐藏位置,以最终提取原始 JSON 内容。 之后,将其转换为 Python dict 以供进一步使用将是微不足道的。 在这种情况下,它位于容器节点 <script type="application/ld+json"> 内。我们的 XPath 可能如下所示:

>>> response.xpath('//script[@type="application/ld+json"]')
[<Selector xpath='//script[@type="application/ld+json"]' data='<script type="application/ld+json">\n{\n  '>,
 <Selector xpath='//script[@type="application/ld+json"]' data='<script type="application/ld+json">\n{\n  '>,
 <Selector xpath='//script[@type="application/ld+json"]' data='<script type="application/ld+json">\n    '>]

这将在 xml 页面中找到每个“脚本”节点,其 属性 为“类型”,为“application/ld +json”。 显然这还不够具体,因为我们找到了三个节点(Selector-wrapped 在我们返回的列表中)。

根据您的分析,我们知道我们的 JSON 必须包含 "@type":"Event",所以让我们的 xpath 对其进行一些子字符串搜索:

>>> response.xpath("""//script[@type="application/ld+json"]/self::node()[contains(text(), '"@type":"Event"')]""")
[<Selector xpath='//script[@type="application/ld+json"]/self::node()[contains(text(), \'"@type":"Event"\')]' data='<script type="application/ld+json">\n    '>]

在这里我们添加了第二个限定符,它表示我们的script 节点 必须包含给定的文本
('self::node()' 显示了一些 XPath 轴魔法,可以在此时引用我们当前的 script 节点 - 而不是它的后代。不过我们将简化它。)
现在我们的返回列表包含一个节点/选择器。正如我们从data= 字符串中看到的,如果我们要extract() 这个,我们现在会 得到一些像&lt;script type="application/ld+json"&gt;[...]&lt;/script&gt; 这样的字符串。 由于我们关心的是节点的内容,而不是节点本身,我们还有一步要走:

>>> response.xpath("""//script[@type="application/ld+json"][contains(text(), '"@type":"Event"')]/text()""")
[<Selector xpath='//script[@type="application/ld+json"][contains(text(), \'"@type":"Event"\')]/text()' data='\n        [\n          \n            \n     '>]

这会返回(一个SelectorList)我们的目标text()。如您所见,我们也可以取消自我参照。 现在,xpath() 总是返回一个SelectorList,但我们有一个小帮手:response.xpath().extract_first() 将在处理它之前获取列表的第一个元素 - 检查它是否存在。 我们可以将此结果放入data 变量中,然后将json.loads(data) 放入 Python 字典并查找我们的值很简单:

>>> events = json.loads(data)
>>> [item['url'] for item in events]
['<url>',
 '<url>',
 '<url>',
 '<url>']

现在您可以将它们变成scrapy.Request(url)s,然后您就会知道如何从那里继续。

.
与往常一样,负责任地爬行并保持“网络”是一个不错的地方。我不认可任何非法行为。
评估自己的权利或获得访问指定目标资源的权限是自己的责任。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 2017-05-03
    相关资源
    最近更新 更多