Python crawlers crawling printer cartridges and component surplus

Source: Internet
Author: User

# Coding: UTF-8

Import re, OS, urllib, string


Def getHtml (url ):

Page = urllib. urlopen (url)

Html = page. read ()

Return html


Def getData (html ):

Cartridge = r'black Print Cartridge & nbsp; (. +? % )'

Car = re. compile (cartridge)

Cartridge = re. findall (car, html)

Rekit = r'maintenance Kit & nbsp; (. +? % )'

Kit = re. compile (rekit)

Kit = re. findall (kit, html)

Print "Cartridge = % s" % Cartridge,

Print "Kit = % s" % Kit


Def getData_TFNRCPRT02 (html ):

Cartridge = R' & nbsp; (. +? % )'

Car = re. compile (cartridge)

Cartridge = re. findall (car, html)

Print "Cartridge = % s" % Cartridge


Def IpStatus (ip ):

Status = OS. system ("ping" + "" + ip)

Return Status


Def getIp (url ):

GetIp = r 'HTTP: // (. + ?) /'

GetIp = re. compile (getIp)

IpAddress = re. findall (GetIp, url)

Ip = ''. join (IpAddress)

Return ip


UrlDir = {

'Http: // 10.135690.171/hp/device/this. LCDispatcher ': 'galprt04 ',

'Http: // 10.135690.172/hp/device/this. LCDispatcher ': 'galprt05 ',

'Http: // 10.153.24.177/hp/device/this. LCDispatcher ': 'szprt11 ',

'Http: // 10.153.24.172/hp/device/this. LCDispatcher ': 'szprt13 ',

'Http: // 10.153.26.179/hp/device/this. LCDispatcher ': 'szprt14 ',

'Http: // 10.186.65.156/hp/device/this. LCDispatcher ': 'tfnrcprt01 ',

'Http: // 10.186.65.101/': 'tfnrcprt3390 ',

'Http: // 10.153.25.200/': 'szprt16'

}


For url in UrlDir:

Ip = getIp (url)

Status = IpStatus (ip)

If Status = 0:

If url = 'HTTP: // 10.186.65.101 /':

Html = getHtml (url)

Print UrlDir [url] + ":",

GetData_TFNRCPRT02 (html)

Elif url = 'HTTP: // 10.153.25.200 /':

Print "SZPRT16 is OK"

Else:

Html = getHtml (url)

Print UrlDir [url] + ":",

GetData (html)

Else:

Print UrlDir [url] + ":" + "Ping fail"



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.