提取与每个URL关联的日期

时间:2016-12-13 12:07:25

标签: python html windows date web-scraping

我无法从每个URL中提取日期,因为类“grid 3”也在许多其他属性中共享。任何形式的指导将不胜感激。

1)Python代码

from bs4 import BeautifulSoup
import re
from urllib.request import urlopen
import requests
.
k = requests.get("http://www.ign.com/tv/reviews")
b = k.text
asoup = BeautifulSoup(b, "html.parser")
.
f = asoup.find_all("div")
.
for item in l:
  print(item)
  print("--"*20)

2)HTML代码

<div class="grid_3">
  <div>December 5, 2016</div>
</div>
<div class="releaseDate grid_3 omega">
  <div class="scoreBox">
    <a class-"scoreBox-link" href="http://...">
    <span class-"scoreBox-score">7.3</span>
  <span class="scoreBox-scorePhrase">Good</span>
    </a>
  </div>
</div>

<div class="clear itemList-item">
<div class="grid_3 alpha">
  <a href="http://...">
  <img class="item-boxArt" src="http://..." alt="Home Again"/>
  </a>
</div>

1 个答案:

答案 0 :(得分:0)

试试这个:

#coding:utf-8

import requests
from lxml import html

response = requests.get("http://www.ign.com/tv/reviews")
tree = html.fromstring(response.content)
items = tree.xpath('//div[@class="clear itemList-item"]')

for item in items:
    link, title = map(lambda x: x.strip(), item.xpath('.//div[@class="up-com grid_7"]/div[@class="item-title"]/h3/a/text() | .//div[@class="up-com grid_7"]/div[@class="item-title"]/h3/a/@href')) 
    date = item.xpath('.//div[@class="grid_3"]/div/text()')[0].strip()
    score_number, score_description = item.xpath('.//div[@class="releaseDate grid_3 omega"]/div[@class="scoreBox"]/a[@class="scoreBox-link"]/span[@class="scoreBox-score"]/text() | .//div[@class="releaseDate grid_3 omega"]/div[@class="scoreBox"]/a[@class="scoreBox-link"]/span[@class="scoreBox-scorePhrase"]/text()')
    print "Title: {}".format(title)
    print "Link: {}".format(link)
    print "Date: {}".format(date)
    print "Score: {} ({})\n".format(score_number, score_description)

<强>结果:

Title: Dragon Ball Super - Champa's Challenge! This Time We Fight With Baseball!
Link: http://www.ign.com/articles/2016/12/13/dragon-ball-super-episode-70-achampas-challenge-this-time-we-fight-with-baseballa-review
Date: December 13, 2016
Score: 8 (Great)

Title: The Walking Dead - Hearts Still Beating
Link: http://www.ign.com/articles/2016/12/11/the-walking-dead-hearts-still-beating-review
Date: December 12, 2016
Score: 7.6 (Good)

Title: Ash vs Evil Dead - Second Coming
Link: http://www.ign.com/articles/2016/12/11/ash-vs-evil-dead-second-coming-review
Date: December 12, 2016
Score: 8.5 (Great)

Title: Star Wars Rebels - Visions and Voices
Link: http://www.ign.com/articles/2016/12/11/star-wars-rebels-visions-and-voices-review
Date: December 11, 2016
Score: 8.8 (Great)

Title: DC's Legends of Tomorrow - The Chicago Way
Link: http://www.ign.com/articles/2016/12/08/dcs-legends-of-tomorrow-the-chicago-way-review
Date: December 9, 2016
Score: 9.2 (Amazing)

Title: Supernatural - Lotus
Link: http://www.ign.com/articles/2016/12/08/supernatural-lotus-review
Date: December 9, 2016
Score: 8.3 (Great)

Title: South Park - The End of Serialization As We Know It
Link: http://www.ign.com/articles/2016/12/07/south-park-the-end-of-serialization-as-we-know-it-review
Date: December 8, 2016
Score: 6.6 (Okay)

Title: Arrow - What We Leave Behind
Link: http://www.ign.com/articles/2016/12/07/arrow-what-we-leave-behind-review
Date: December 8, 2016
Score: 8.9 (Great)

Title: Vikings - The Vision
Link: http://www.ign.com/articles/2016/12/07/vikings-the-vision-review
Date: December 8, 2016
Score: 7.8 (Good)

Title: The Flash - The Present
Link: http://www.ign.com/articles/2016/12/06/the-flash-the-present-review
Date: December 7, 2016
Score: 8.6 (Great)

Title: Dragon Ball Super - Goku vs Arale! A Ridiculous Battle Will End the Earth?!
Link: http://www.ign.com/articles/2016/12/05/dragon-ball-super-episode-69-goku-vs-arale-a-ridiculous-battle-will-end-the-earth-review
Date: December 6, 2016
Score: 6.5 (Okay)

Title: Westworld - The Bicameral Mind
Link: http://www.ign.com/articles/2016/12/04/westworld-the-bicameral-mind-review
Date: December 5, 2016
Score: 8.5 (Great)

Title: The Walking Dead - Sing Me a Song
Link: http://www.ign.com/articles/2016/12/04/the-walking-dead-sing-me-a-song-review
Date: December 5, 2016
Score: 7.3 (Good)

Title: Ash vs Evil Dead - Home Again
Link: http://www.ign.com/articles/2016/12/04/ash-vs-evil-dead-home-again-review
Date: December 5, 2016
Score: 9 (Amazing)

Title: Star Wars Rebels - An Inside Man
Link: http://www.ign.com/articles/2016/12/03/star-wars-rebels-an-inside-man-review
Date: December 4, 2016
Score: 8.8 (Great)

Title: DC's Legends of Tomorrow - Invasion!
Link: http://www.ign.com/articles/2016/12/01/dcs-legends-of-tomorrow-invasion-review
Date: December 2, 2016
Score: 8.8 (Great)

Title: South Park - Not Funny
Link: http://www.ign.com/articles/2016/11/30/south-park-not-funny-review
Date: December 1, 2016
Score: 8.6 (Great)

Title: Arrow - Invasion!
Link: http://www.ign.com/articles/2016/11/30/arrow-invasion-review
Date: December 1, 2016
Score: 8.7 (Great)

Title: Vikings - The Outsider
Link: http://www.ign.com/articles/2016/11/30/vikings-the-outsider-review
Date: December 1, 2016
Score: 8.5 (Great)

Title: Lethal Weapon - Can I Get A Witness?
Link: http://www.ign.com/articles/2016/11/30/lethal-weapon-can-i-get-a-witness-review
Date: December 1, 2016
Score: 7.2 (Good)

Title: The Flash - Invasion!
Link: http://www.ign.com/articles/2016/11/29/the-flash-invasion-review
Date: November 30, 2016
Score: 8.4 (Great)

Title: Marvel's Agents of S.H.I.E.L.D. - Deals with Our Devils
Link: http://www.ign.com/articles/2016/11/29/marvels-agents-of-shield-deals-with-our-devils-review
Date: November 30, 2016
Score: 8.3 (Great)

Title: Incorporated - Vertical Mobility
Link: http://www.ign.com/articles/2016/11/29/incorporated-vertical-mobility-review
Date: November 29, 2016
Score: 7 (Good)

Title: Dragon Ball Super - Come Forth, Shenron! Whose Wish Will Be Granted?!
Link: http://www.ign.com/articles/2016/11/29/dragon-ball-super-episode-68-acome-forth-shenron-whose-wish-will-be-granteda-review
Date: November 29, 2016
Score: 7.5 (Good)

Title: Supergirl - Medusa
Link: http://www.ign.com/articles/2016/11/28/supergirl-medusa-review
Date: November 29, 2016
Score: 8.4 (Great)