Попытка захватить определенные элементы - PullRequest
0 голосов
/ 22 апреля 2019

Я новичок в модуле lxml в Python. Я пытаюсь проанализировать данные с веб-сайта: https://weather.com/weather/tenday/l/USCA1037:1:US

Я пытаюсь взять текст:

<span classname="narrative" class="narrative">
  Cloudy. Low 49F. Winds WNW at 10 to 20 mph.
</span>

Тем не менее, я путаю свой xpath.

Если быть точным, расположение этой строки

//*[@id="twc-scrollabe"]/table/tbody/tr[4]/td[2]/span

Я пытался сделать следующее

import requests

import lxml.html

from lxml import etree



html = requests.get("https://weather.com/weather/tenday/l/USCA1037:1:US")

element_object = lxml.html.fromstring(html.content)  # htmlelement object returns bytes
  # element_object has root of <html>

table = element_object.xpath('//div[@class="twc-table-scroller"]')[0]
day_of_week = table.xpath('.//span[@class="date-time"]/text()')  # returns list of items from "dates-time"
dates = table.xpath('.//span[@class="day-detail clearfix"]/text()')

td = table.xpath('.//tbody/tr/td/span[contains(@class, "narrative")]')
print td

  # print td displays an empty list.  

Я бы хотел, чтобы моя программа также анализировала: «Облачно. Низкий 49F. Ветер дует со скоростью 10-20 миль в час.»

Пожалуйста, помогите ...

Ответы [ 2 ]

0 голосов
/ 22 апреля 2019

Если вы хотите получить текст, например Sunny. High 66F. Winds WNW at 5 to 10 mph., вы можете получить его из атрибутов заголовка <td>.

. Это должно работать.

td = table.xpath('.//tbody/tr/td[@class="description"]/@title')
0 голосов
/ 22 апреля 2019

Некоторые <td> имеют title= с описанием

import requests
import lxml.html

html = requests.get("https://weather.com/weather/tenday/l/USCA1037:1:US")

element_object = lxml.html.fromstring(html.content)
table = element_object.xpath('//div[@class="twc-table-scroller"]')[0]

td = table.xpath('.//tr/td[@class="twc-sticky-col"]/@title')
print(td)

Результат

['Mostly cloudy skies early, then partly cloudy after midnight. Low 48F. Winds SSW at 5 to 10 mph.', 
 'Mainly sunny. High 66F. Winds WNW at 5 to 10 mph.', 
 'Sunny. High 71F. Winds NW at 5 to 10 mph.', 
 'A mainly sunny sky. High 69F. Winds W at 5 to 10 mph.', 
 'Some clouds in the morning will give way to mainly sunny skies for the afternoon. High 67F. Winds WSW at 5 to 10 mph.', 
 'Considerable clouds early. Some decrease in clouds later in the day. High 67F. Winds WSW at 5 to 10 mph.', 
 'Partly cloudy. High near 65F. Winds WSW at 5 to 10 mph.', 
 'Cloudy skies early, then partly cloudy in the afternoon. High 61F. Winds WSW at 10 to 20 mph.', 
 'Sunny skies. High 62F. Winds WNW at 10 to 20 mph.', 
 'Mainly sunny. High 61F. Winds WNW at 10 to 20 mph.', 
 'Sunny along with a few clouds. High 64F. Winds WNW at 10 to 15 mph.', 
 'Mostly sunny skies. High around 65F. Winds WNW at 10 to 15 mph.', 
 'Mostly sunny skies. High 66F. Winds WNW at 10 to 20 mph.', 
 'Mainly sunny. High around 65F. Winds WNW at 10 to 20 mph.', 
 'A mainly sunny sky. High around 65F. Winds WNW at 10 to 20 mph.']

В HTML нет <tbody>, но веб-браузер может отображать его в DevTool - такне используйте tbody в xpath.

Часть текста <span></span>, часть <span><span></span></span>

import requests
import lxml.html

html = requests.get("https://weather.com/weather/tenday/l/USCA1037:1:US")

element_object = lxml.html.fromstring(html.content)
table = element_object.xpath('//div[@class="twc-table-scroller"]')[0]

td = table.xpath('.//tr/td//span/text()')
print(td)

Результат

['Tonight', 'APR 21', 'Partly Cloudy', '--', '48', '10', '%', 'SSW 7 mph ', '85', '%', 
 'Mon', 'APR 22', 'Sunny', '66', '51', '10', '%', 'WNW 9 mph ', '67', '%', 
 'Tue', 'APR 23', 'Sunny', '71', '53', '0', '%', 'NW 8 mph ', '59', '%', 
 'Wed', 'APR 24', 'Sunny', '69', '52', '10', '%', 'W 9 mph ', '71', '%', 
 'Thu', 'APR 25', 'Partly Cloudy', '67', '51', '10', '%', 'WSW 9 mph ', '71', '%', 
 'Fri', 'APR 26', 'Partly Cloudy', '67', '51', '10', '%', 'WSW 9 mph ', '69', '%', 
 'Sat', 'APR 27', 'Partly Cloudy', '65', '50', '10', '%', 'WSW 9 mph ', '71', '%',   
 'Sun', 'APR 28', 'AM Clouds/PM Sun', '61', '49', '20', '%', 'WSW 13 mph ', '75', '%', 
 'Mon', 'APR 29', 'Sunny', '62', '48', '10', '%', 'WNW 14 mph ', '63', '%', 
 'Tue', 'APR 30', 'Sunny', '61', '49', '0', '%', 'WNW 14 mph ', '61', '%', 
 'Wed', 'MAY 1', 'Mostly Sunny', '64', '50', '0', '%', 'WNW 12 mph ', '60', '%', 
 'Thu', 'MAY 2', 'Mostly Sunny', '65', '50', '0', '%', 'WNW 12 mph ', '61', '%', 
 'Fri', 'MAY 3', 'Mostly Sunny', '66', '51', '0', '%', 'WNW 13 mph ', '61', '%', 
 'Sat', 'MAY 4', 'Sunny', '65', '51', '0', '%', 'WNW 14 mph ', '62', '%', 
 'Sun', 'MAY 5', 'Sunny', '65', '51', '0', '%', 'WNW 14 mph ', '63', '%']
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...