【问题标题】:why my xpath always get nothing like "[]"?为什么我的 xpath 总是得到像“[]”这样的东西?
【发布时间】:2019-01-07 14:11:10
【问题描述】:

我是爬行网页的新手。我的代码正在尝试获取网站的时间。我找到了位置并尝试使用 xpath 来获取 text()。但我的代码总是返回“[]”。我错过了什么吗?

# -*- coding: utf-8 -*-
import urllib
from bs4 import BeautifulSoup

from lxml import etree
from lxml import html
import requests
headers= { 'User-Agent' : 'User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36' }

tree = requests.get('https://www.time.gov/',headers=headers).content#.decode('utf-8')


doc_tree = etree.HTML(tree)
links = doc_tree.xpath('//div[@id="lzTextSizeCache"]/div[@class="lzswftext"]/text()')

print links

html代码的位置是:

<div class="lzswftext" style="padding: 0px; overflow: visible; width: auto; height: auto; font-weight: bold; font-style: normal; font-family: Arial, Verdana; font-size: 50px; white-space: pre; display: none;">09:37:26 a.m. </div>

【问题讨论】:

  • 能否提供相关的HTML sn-p?
  • 这意味着它找不到您在页面中提供的模式。期望的输出是什么?如果是网站上显示的时间,您会知道 html 代码中没有硬编码,而是 Javascript

标签: python xpath web-crawler


【解决方案1】:

您不会得到时间,因为该请求没有时间:

这是因为网页再次请求获取时间。在这种特殊情况下,请求是“https://www.time.gov/actualtime.cgi?disablecache=1546870424051&lzbc=wr1d55”,它获得了这个 html:

<timestamp time="1546870996756222" delay="1545324126332171"/>

有一些 javascript 代码可以将该时间戳转换为日期,您可以使用 python 模拟它:

In [28]: import requests                                                                                                                                                                                            

In [29]: from datetime import datetime                                                                                                                                                                              

In [30]: res = requests.get('https://www.time.gov/actualtime.cgi?disablecache=1546870424051&__lzbc__=wr1d55')                                                                                                       
2019-01-07 09:34:15 [urllib3.connectionpool] DEBUG: Starting new HTTPS connection (1): www.time.gov:443
2019-01-07 09:34:16 [urllib3.connectionpool] DEBUG: https://www.time.gov:443 "GET /actualtime.cgi?disablecache=1546870424051&__lzbc__=wr1d55 HTTP/1.1" 200 None

In [31]: from bs4 import BeautifulSoup 
    ...:                                                                                                                                                                                                            

In [32]: soup = BeautifulSoup(res.text, 'html.parser')                                                                                                                                                              

In [34]: soup.timestamp['time']                                                                                                                                                                                     
Out[34]: '1546871656757021'

In [35]: ts = soup.timestamp['time']                                                                                                                                                                                

In [38]: ts = int(soup.timestamp['time'])                                                                                                                                                                           

In [39]: ts /= 1000000     # because timestamp is in microseconds                                                                                                                                                                                         

In [40]: print(datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')) 
    ...:                                                                                                                                                                                                            
2019-01-07 14:34:16

要获取本地区域的时间,请阅读:Convert UTC datetime string to local datetime with Python

这可能是一个过于复杂的解决方案,您也可以使用 Selenium 或 scrapy+splash 之类的东西来获得与您在浏览器中看到的相同的东西。

【讨论】:

  • 非常详细,非常感谢
【解决方案2】:

您的项目是异步生成的

  • 页面需要一些时间来生成您要查找的项目。您可以在页面的源代码中看到一些指令,如setTimeout("updatexearthImage()", 10000);
  • 同样在源代码中,您可以看到您的项目不是 initial 页面的一部分。例如当做 curl

解决方案

尝试使用运行 Javascript 的无头浏览器,您可能还需要在代码中包含一些 延迟 以使页面完全呈现。例如Puppeteer 或者Selenium

【讨论】:

    猜你喜欢
    • 2018-09-23
    • 2020-10-24
    • 2013-09-20
    • 1970-01-01
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    相关资源
    最近更新 更多