我正试图从squawka网页上抓取埃弗顿和阿斯顿维拉之间比赛拍摄的x和y坐标:http://epl.squawka.com/everton-vs-aston-villa/18-10-2014/english-barclays-premier-league/matches。
我使用Firebug元素检查器来获取圆圈的X路径(例如/ html / body / div [2] / div [3] / div [2] / div [1] / div / div [ 15] / DIV [2] / DIV [3] / DIV [1] / DIV [3] / DIV [1] / DIV [2] / SVG / g的[22] /圆圈)。每个镜头圆的像素坐标包含在cx和cy属性中。
我曾尝试使用Python中的scrapy模块来删除这些数字,但没有成功。我对此非常陌生,并且基本上已经调整了scrapy教程中的代码。项目文件:
import scrapy
class SquawkaItem(scrapy.Item):
cx = scrapy.Field()
cy = scrapy.Field()
蜘蛛文件:
import scrapy
from squawka.items import SquawkaItem
class SquawkaSpider(scrapy.Spider):
name = "squawka"
allowed_domains = ["squawka.com"]
start_urls = ["http://epl.squawka.com/everton-vs-aston-villa/18-10-2014/english-barclays-premier-league/matches"]
def parse(self, response):
for sel in response.xpath('/html/body/div/div/div/div/div/div/div/div/div/div/div/div/svg/g/circle'):
cx = sel.xpath('[@cx]').extract()
cy = sel.xpath('[@cy]').extract()
print cx, cy
当我在linux终端中运行这个蜘蛛时,使用'scrapy crawl squawka'命令,我得到以下输出:
2014-10-26 12:49:53+0000 [scrapy] INFO: Scrapy 0.25.0-222-g675fd5b started (bot: squawka)
2014-10-26 12:49:53+0000 [scrapy] INFO: Optional features available: ssl, http11, boto, django
2014-10-26 12:49:53+0000 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'squawka.spiders', 'SPIDER_MODULES': ['squawka.spiders'], 'BOT_NAME': 'squawka'}
2014-10-26 12:49:54+0000 [scrapy] INFO: Enabled extensions: LogStats, TelnetConsole, CloseSpider, CoreStats, SpiderState
2014-10-26 12:49:55+0000 [scrapy] INFO: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMiddleware, CookiesMiddleware, ChunkedTransferMiddleware, DownloaderStats
2014-10-26 12:49:55+0000 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2014-10-26 12:49:55+0000 [scrapy] INFO: Enabled item pipelines:
2014-10-26 12:49:55+0000 [squawka] INFO: Spider opened
2014-10-26 12:49:55+0000 [squawka] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2014-10-26 12:49:55+0000 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2014-10-26 12:49:56+0000 [squawka] DEBUG: Crawled (200) <GET http://epl.squawka.com/everton-vs-aston-villa/18-10-2014/english-barclays-premier-league/matches> (referer: None)
2014-10-26 12:49:56+0000 [squawka] INFO: Closing spider (finished)
2014-10-26 12:49:56+0000 [squawka] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 300,
'downloader/request_count': 1,
'downloader/request_method_count/GET': 1,
'downloader/response_bytes': 16169,
'downloader/response_count': 1,
'downloader/response_status_count/200': 1,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2014, 10, 26, 12, 49, 56, 402920),
'log_count/DEBUG': 1,
'log_count/INFO': 3,
'response_received_count': 1,
'scheduler/dequeued': 1,
'scheduler/dequeued/memory': 1,
'scheduler/enqueued': 1,
'scheduler/enqueued/memory': 1,
'start_time': datetime.datetime(2014, 10, 26, 12, 49, 55, 261954)}
2014-10-26 12:49:56+0000 [squawka] INFO: Spider closed (finished)
正如您所看到的那样,它没有抓取任何网页,也没有输出数据。我不知道如何更改我的代码以获取我想要的数据。我会非常感激地接受我的代码或其他技术变更的任何想法。感谢。
答案 0 :(得分:0)
答案 1 :(得分:0)
我已经获得了我现在需要的数据 - 感谢您的建议。最后我最终使用了Selenium webdriver和Beautifulsoup。这是我的代码:
from selenium import webdriver
from bs4 import BeautifulSoup
driver = webdriver.Firefox()
driver.get("http://epl.squawka.com/everton-vs-aston-villa/18-10-2014/english-barclays- premier-league/matches")
#find the elements which bring up the shots svg image
inputElement = driver.find_element_by_id("mc-stat-shot")
inputElement.click()
inputElement = driver.find_element_by_id("team2-select")
inputElement.click()
pageSource = driver.page_source
soup = BeautifulSoup(pageSource)
for circle in soup.find_all('circle'):
if circle['r'] == '6.5':
x = circle['cx']
y = circle['cy']
print x, y
else:
continue
driver.quit()
代码使用Selenium单击网页上的元素以显示正确的svg图像(整个音高,所有镜头都标记为圆圈)。间距svg的尺寸为480x366像素。然后我存储页面源并使用Beautifulsoup获取我感兴趣的(x和y坐标)。