一 存入隊列
import urllib.requestimport queueimport req = queue.Queue()headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}url = "http://news.163.com/rank/"url = urllib.request.Request(url , headers = headers)data = urllib.request.urlopen(url).read()data = data.decode('GBK')newlist=[]newlist = re.compile(r'</span><a href="(.*?)">(.*?)</a>',re.DOTALL).findall(data)Length = len(newlist)for i in range(0, Length): q.put(newlist[i])while not q.empty(): print(q.get())
二 儲存本地
import urllib.request import reheaders = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}url = "http://news.163.com/rank/"url = urllib.request.Request(url , headers = headers)data = urllib.request.urlopen(url).read() data = data.decode('GBK')newlist=[] newlist = re.compile(r'</span><a href="(.*?)">(.*?)</a>',re.DOTALL).findall(data)Length = len(newlist) for i in range(0, Length): print("NEW%d:%s\n"%(i+1, newlist[i])) file_object = open("news.txt","a") file_object.write("%s\n"% newlist) file_object.close()