Scrapy Recursive Download website

Source: Internet
Author: User
Tags xpath

? # Encoding:utf-8
Import OS
Import re
Import subprocess
Import Sys

Import Chardet
Import Scrapy
From scrapy.http import Request
From Scrapy.selector import Htmlxpathselector
From Scrapy.spider import Basespider
From Scrapy.utils.url import URLJOIN_RFC

From Mychardet Import *

# Print sys.getdefaultencoding ()

# Print Sys.path

Def get_default_to_codec ():
Return mytogb18030

Def GetFirst (a):
# print ' Enter GetFirst ', repr (a)
If a = = None:
return U '
Elif len (a) = = 0:
return U '
b = A[0]
# print repr (b) #, Chardet.detect (b)
Return b

Class Greasemonkey1spider (Scrapy. Spider):
Name = "Test"
Allowed_domains = ["localhost"]
Start_urls = (
' Http://localhost/test ',
)

def parsecontext (self, Response):
# print "Enter parsecontext:", Response.url
HXS = response
sel = Hxs.xpath ('//title/text () ')
If sel! = None:
Titles = Sel.extract ()
If Len (titles) > 0:title = titles[0]
Else:title = ' '

sel = Hxs.xpath ('/html/body ')
If sel! = None:
Bodys = Sel.extract ()
If Len (bodys) > 0:body = bodys[0]
Else:body = ' '
# print title, repr (body)

Def parse (self, Response):
BaseURL = Response.url
print ' BaseURL = ', BaseURL
Self.parsecontext (response)

        hxs  = Response.xpath (R '//a ')
         for Path in HXS:
            titles = GetFirst (Path.xpath (R ' text () '). Extract ())
             URLs = GetFirst (Path.xpath (R ' @href '). Extract ())
#              print titles, URLs
            item_ url = urljoin_rfc (baseurl, URLs)
            yield Request (Item_url,callback=self.parse)

if __name__ = = ' __main__ ':
cmd = ' E:\Python27\Scripts\scrapy.exe crawl--nolog test '
CWD = Os.path.split (__file__) [0]
p = subprocess. Popen (Cmd.split (), stdout=subprocess. PIPE, Stderr=subprocess. PIPE, Shell=false, CWD=CWD)
While None = = P.poll ():
Out, err = P.communicate ()
# print ' Out, err ', off, err, repr (out), repr (ERR)
If err:
Print Err
Elif out:
Print out

Print P.returncode

# While not P.poll ():
# Print P.stdout.read ()
# Print P.stderr.read ()

Scrapy Recursive Download website

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.