国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 枝江市| 滨州市| 阳信县| 沾益县| 固阳县| 定日县| 尼勒克县| 长葛市| 惠安县| 出国| 兴城市| 比如县| 九江市| 开原市| 安达市| 莱芜市| 寻乌县| 定兴县| 岐山县| 宜城市| 客服| 宁乡县| 鹿邑县| 西昌市| 额尔古纳市| 盐亭县| 新巴尔虎右旗| 澄城县| 右玉县| 洪泽县| 鱼台县| 阿克苏市| 五大连池市| 凉山| 云南省| 婺源县| 屏东县| 张家口市| 高唐县| 丰都县| 冕宁县|