Python crawler scrapy Framework self-built IP proxy pool __python

Source: Internet
Author: User
Tags xpath

1, HTTP://WWW.XICIDAILI.COM/WT domestic free agent website

2, using Scrapy crawl the site's IP address and port, write txt document

3, write script test txt document IP address and port is available

4, the available IP address and port input TXT document

————————————————————————
1. Write Item class
Because we only need IP address and port, so write only one attribute can

#-*-Coding:utf-8-*-

# Define Here's models for your scraped items # to documentation in
:
# http:/ /doc.scrapy.org/en/latest/topics/items.html

Import Scrapy


class Ipitem (scrapy. Item):
    # define the fields for your item here like:
    # name = Scrapy. Field ()
    Pass

class Ipinfoitem (scrapy. Item):
    ip=scrapy. Field ()

2, the preparation of spider

#-*-Coding:utf-8-*-
import scrapy
import sys
sys.path.append ("D:\\pycodes\\ip") from
Ip.items Import Ipinfoitem

class Ipspider (scrapy. Spider):
    name = ' Ip '

    start_urls = []
    #爬取5页网站的IP for
    i in range (1,6):
        start_urls.append (' http:// www.xicidaili.com/wt/' +str (i))

    def parse (self, Response):

        item = Ipinfoitem () for

        sel in Response.xpath ('//tr '):
            ip= Sel.xpath ('.//td[2]/text () '). Extract_first () Port=sel.xpath (
            './/td[3]/ Text () "). Extract_first ()
            item[' IP ']=str (IP) +": "+str (port)

            yield item

3, the preparation of pipeline

#-*-Coding:utf-8-*-

# Define Your item pipelines here
#
Don ' t forget to add your pipeline to the ITEM_PI Pelines setting
# see:http://doc.scrapy.org/en/latest/topics/item-pipeline.html


class Ippipeline (object):
    def process_item (self, item, spider): Return
        Item


class Ipinfopipeline (object):


    def process_item ( Self,item,spider):
        try:
        #我们只需要IP地址与端口, so just write the dictionary value into TXT file
            content = item[' IP '
            open ("Xinresult.txt "," a "). Write (content+" \ n ")

        except: Pass return
        item

So far, we've climbed down 5 pages of IP from the Web site and need to write a script to test

import requests alive_ip=[] def test_alive (proxy): Global alive_ip for Proxies_be in P
        Roxy: #request中的IP地址需要以下列形式的参数写进函数 proxies={"http":p Roxies_be} print ("Testing: {}". Format (proxies)) Try:r = Requests.get ("http://www.baidu.com", proxies=proxies,timeout=2) if r.status_code==2
                00:print ("successful, IP {}". Format (proxies)) Alive_ip.append (proxies_be) Else: Print ("Failed") Except:print ("Failed") def out_file (alive_ip=[]): With open ("Alive_ip.txt", "W") as F:for IP in alive_ip:f.write (str (IP) + "\ n") print ("Output complete") def test (filename= "blank.t 

        XT "): with open (filename," R ") as F:lines = F.readlines () proxys=list (Map (Lambda X:x.strip (), lines)) Test_alive (Proxys) out_file (alive_ip) test ("Xinresult.txt") 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.