国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

實(shí)戰(zhàn) 使用scrapy 爬取代理 并保存到數(shù)據(jù)庫

2019-11-06 07:18:51
字體:
供稿:網(wǎng)友

   爬取網(wǎng)站: www.PRoxy360.com

   爬取思路: 1.分析首頁,2 分析要爬取的目標(biāo) 3,保存數(shù)據(jù)

   1 首先分析頁面

開始爬取

創(chuàng)建爬蟲項(xiàng)目

scrapy startproject daili

創(chuàng)建爬蟲

cd daili

scrapy genspider dd proxy360.cn

創(chuàng)建了一個(gè)名叫dd的爬蟲

在 items.py 下設(shè)置需要爬取的內(nèi)容

這里爬取ipimport scrapyclass DailiItem(scrapy.Item):    # define the fields for your item here like:    # name = scrapy.Field()    ip =scrapy.Field()    pass然后進(jìn)入爬蟲目錄在spiders下的 dd.py
# -*- coding: utf-8 -*-import scrapyfrom daili.items import DailiItem  #導(dǎo)入item.py定義的類名class DdSpider(scrapy.Spider):    name = "dd"    allowed_domains = ["www.proxy360.cn"]
    nations = ['Brazil','Chain','Japan','Vietanm'] #定義好要爬取的名字的后綴    start_urls = []    for nation in nations:  使用for 循環(huán)依次加載上        start_urls.append('http://www.proxy360.cn/Region/'+nation  )    def parse(self, response):    #找到要爬取div        sc = response.xpath('//div[@class="proxylistitem"]')        for sub in sc:              開始依次爬取            item = DailiItem()     #調(diào)用item定義的類名            item["ip"]= sub.xpath('.//span[1]/text()').extract()[0]            yield item            爬取的內(nèi)容返回到pipelines。py里配置  setting.py
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
ITEM_PIPELINES = {    'daili.pipelines.DailiPipeline': 300,}進(jìn)入到pipelines.py
import pyMySQL   # 使用pymysql 連接 如果沒有  使用pip install pymysql 下載class DailiPipeline(object):    def __init__(self):        self.conn = pymysql.connect(host="127.0.0.1",user ="root",passWord= "root",db="get")       連接數(shù)據(jù)庫  host 連接地址 user 賬號(hào)  password密碼     def process_item(self, item, spider):        try:            title = item["ip"] #獲取 從dd。py傳遞過來的 內(nèi)容            sql ="insert into daili(ip) VALUES ('"+title+"')"  傳到數(shù)據(jù)庫            print sql            self.conn.query(sql)            return item        except Exception as e:            pass        def close_spider(self):            self.conn.close()  關(guān)閉數(shù)據(jù)庫連接


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 利津县| 和田县| 万荣县| 西藏| 大渡口区| 招远市| 无为县| 基隆市| 东安县| 手机| 临武县| 卓尼县| 申扎县| 黑水县| 南华县| 祁东县| 敦化市| 交口县| 呼图壁县| 昭觉县| 宽城| 邢台县| 吉安县| 武威市| 镇赉县| 福安市| 兰考县| 保康县| 普格县| 如东县| 交口县| 营山县| 泽库县| 信丰县| 张家口市| 秀山| 绥阳县| 略阳县| 汉阴县| 遂宁市| 从化市|