Python大数据之从网页上爬取数据的方法详解大数据之从网页上爬取数据的方法详解
主要介绍了Python大数据之从网页上爬取数据的方法,结合实例形式详细分析了Python爬虫爬取网页数据的相关
操作技巧,需要的朋友可以参考下
本文实例讲述了Python大数据之从网页上爬取数据的方法。分享给大家供大家参考,具体如下:
myspider.py :
#!/usr/bin/python
# -*- coding:utf-8 -*-
from scrapy.spiders import Spider
from lxml import etree
from jredu.items import JreduItem
class JreduSpider(Spider):
name = 'tt' #爬虫的名字,必须的,唯一的
allowed_domains = ['sohu.com']
start_urls = [
'http://www.sohu.com'
]
def parse(self, response):
content = response.body.decode('utf-8')
dom = etree.HTML(content)
for ul in dom.xpath("//div[@class='focus-news-box']/div[@class='list16']/ul"):
lis = ul.xpath("./li")
for li in lis:
item = JreduItem() #定义对象
if ul.index(li) == 0:
strong = li.xpath("./a/strong/text()")
li.xpath("./a/@href")
item['title']= strong[0]
item['href'] = li.xpath("./a/@href")[0]
else:
la = li.xpath("./a[last()]/text()")
item['title'] = la[0]
item['href'] = li.xpath("./a[last()]/href")[0]
yield item
items.py :
# -*- coding: utf-8 -*-
# Define here the models for your scraped items
#
评论0