• scrapy 博客爬取


    item.py

    import scrapy
    
    
    class FulongpjtItem(scrapy.Item):
        # define the fields for your item here like:
        name = scrapy.Field()
        url = scrapy.Field()
        hits = scrapy.Field()
        comment = scrapy.Field()

    pipeline.py

    import pymysql
    from pymysql import connections
    class FulongpjtPipeline(object):
        def __init__(self):
            self.conn = pymysql.connect(host='127.0.0.1', user='root', passwd='123456', db='mydb')
            self.cursor = self.conn.cursor()
    
        def process_item(self, item, spider):
            for j in range(0,len(item['name'])):
                name = item['name'][j]
                url = item['url'][j]
                hits = item['hits'][j]
                comment = item['comment'][j]
                sql = "insert into boke(name,url,hits,comment) VALUES(%s,%s,%s,%s)"
                self.cursor.execute(sql,(name,url,hits,comment,))
                self.conn.commit()
            return item
        def close_spider(self,spider):
            self.conn.close()

    spd.py

    import scrapy
    from Fulongpjt.items import FulongpjtItem
    from scrapy.http import Request
    import re
    import urllib.request
    class MyspdSpider(scrapy.Spider):
        name = "myspd"
        allowed_domains = ["hexun.com"]
        start_urls = ['http://hexun.com/']
        uid = '19940007'
        def start_requests(self):
            yield Request('http://'+str(self.uid)+'.blog.hexun.com/p1/default.html',headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36 QIHU 360EE'})
    
        def parse(self, response):
            item = FulongpjtItem()
            item['name'] = response.xpath('//span[@class="ArticleTitleText"]/a/text()').extract()
            item['url'] = response.xpath('//span[@class="ArticleTitleText"]/a/@href').extract()
            part1 = '''<script type="text/javascript" src="(http://click.tool.hexun.com/.*?)">'''
            hcurl = re.compile(part1).findall(str(response.body))[0]
            headers2 = ('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36 QIHU 360EE')
            opener =urllib.request.build_opener()
            opener.addheaders =[headers2]
            urllib.request.install_opener(opener)
            data = urllib.request.urlopen(hcurl).read()
            part2 = "clickd*?','(d*?)'"
            part3 = "commentd*?','(d*?)'"
            item['hits'] = re.compile(part2).findall(str(data))
            item['comment'] = re.compile(part3).findall(str(data))
            yield item
            # 提取文章总页数
            part4 = 'blog.hexun.com/p(.*?)/'
            data2=re.compile(part4).findall(str(response.body))
            if len(data2)>2:
                totalurl = data2[-2]
            else:
                totalurl = 1
            for i in range(2,int(totalurl)+1):
                next_url = 'http://'+str(self.uid)+'.blog.hexun.com/p'+str(i)+'/default.html'
                yield Request(next_url,callback=self.parse,headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36 QIHU 360EE'})
  • 相关阅读:
    实用SQL命令收集
    ZedGraph在Asp.net中的应用
    怎样制作一张万能的Win XP安装光盘
    【转】poj 1823 hotel 线段树【Good】
    【转】unique()函数
    POJ1389Area of Simple Polygons
    【转】poj 1823
    【转】POJ 1177 (线段树+离散化+扫描线) 详解
    【转】POJ各题算法分类和题目推荐
    【转】sort()函数定义在头文件<algorithm>中,它把容器中的数据重新排序成非递减序列
  • 原文地址:https://www.cnblogs.com/Erick-L/p/6841027.html
Copyright © 2020-2023  润新知