python實現同服網站地址擷取,python網站地址擷取
說明:程式使用http://s.tool.chinaz.com/same此網站查詢的結果,使用python簡單的實現抓取結果
先隨便查詢一個結果,抓包分析,
使用python模仿post表單,使用Regex匹配結果
代碼如下:
# -*- coding: utf-8 -*- import urllibimport urllib2import reimport sys#get url in the same ipdef get_url(url): #set header info headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36', 'Referer': 'http://s.tool.chinaz.com/same' } postdata = urllib.urlencode({'s':url}) req = urllib2.Request('http://s.tool.chinaz.com/same',postdata,headers) try: result = urllib2.urlopen(req) except: print 'Failed to open url,you can try again...' return fweb = result.read() #.</span> <a href='http://www.31hzp.com' pattern = re.compile(r'</span> <a href=\'(.+?)\'') match = pattern.findall(fweb) filename = str(url).replace(':', '').replace('\\', '') fp = open(filename+'.txt','w') if match: for m in match: fp.write(m) fp.write('\n') print m else: print 'find nothing...' fp.close()#usagedef usage(name): #www.31jmw.com print '%s www.xxx.com'%name sys.exit(1)#entry pointif __name__ == '__main__': if len(sys.argv) != 2: usage(sys.argv[0]) print 'start...' url = "".join(sys.argv[1]) #取出列表中的字串 #print url get_url(url) print 'end...'
測試結果如下:
F:\mycode\python\pytest\src>ipsamescan.py www.31jmw.comstart...http://www.31hzp.comhttp://100ec.cnhttp://ec100.cnhttp://toocle.cnhttp://www.31jmw.comhttp://www.31expo.comhttp://www.toocle.cnhttp://561288.comhttp://www.toocle.com.cnhttp://www.31metals.comhttp://31expo.comhttp://www.100ec.cnend...
怎用python實現擷取操作網頁的資訊?
用selenium 。或者前台實現也行。。
或者用個gui,在裡面展示html頁面。然後捕獲。
怎使用python抓取網頁並實現一些提交操作?
下面這個程式是抓取網頁的一個例子,MyOpener類是為了類比瀏覽器用戶端,並採用隨機選取的方式以防網站將你認為是機器人。
MyFunc函數抓取你指定的url,並提取了其中的href連結,圖片的擷取類似,一般是<img src=xxx>這樣的形式,其他的功能應該也不難,去網上搜下應該有些例子。
import re
from urllib import FancyURLopener
from random import choice
user_agents = [
'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11',
'Opera/9.25 (Windows NT 5.1; U; en)',
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
'Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu)',
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12',
'Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9'
]
class MyOpener(FancyURLopener, object):
version = choice(user_agents)
def MyFunc(url):
myopener = MyOpener()
s = myopener.open(url).read()
ss=s.replace("\n"," ")
urls=re.findall(r"<a.*?href=.*?<\/a>",ss,re.I)#尋找href連結
for i in urls:
do sth.