【发布时间】: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