资源描述:
《python抓取页面、pthon爬虫参考资料》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、python中如何提取网页正文啊谢谢importurllib.request url="http://google.cn/" response=urllib.request.urlopen(url) page=response.read() python提取网页中的文本1.importos,sys,datetime 2.importhttplib,urllib,re 3.fromsgmllibimportSGMLParser 4. 5.importtypes 6. 7.classH
2、tml2txt(SGMLParser): 8. defreset(self): 9. self.text='' 10. self.inbody=True 11. SGMLParser.reset(self) 12. defhandle_data(self,text): 13. ifself.inbody: 14. self.text+=text 15. 16. defstart
3、_head(self,text): 17. self.inbody=False 18. defend_head(self): 19. self.inbody=True 20. 21. 22.if__name__=="__main__": 23. parser=Html2txt() 24. parser.feed(urllib.urlopen("http://icode.csdn.net").read()) 25. pars
4、er.close() 26. printparser.text.strip() python下载网页importhttplib conn=httplib.HTTPConnection("www.baidu.com")conn.request("GET","/index.html")r1=conn.getresponse()printr1.status,r1.reasondata=r1.read()printdataconn.close用python下载网页,超级简单!fromurllib
5、importurlopenwebdata=urlopen("").read()printwebdata深入python里面有python 下载网页内容,用python的pycurl模块实现1.用python下载网页内容还是很不错的,之前是使用urllib模块实验的,但听说有pycurl这个模块,而且比urllib好,所以尝试下,废话不说,以下是代码2.3.4.#!/usr/bin/envpython5.#-*-coding:utf-8-*-6.importStringIO7.importpycurl8
6、.1.defwritefile(fstr,xfilename): f=open(xfilename,'w') f.write(fstr) f.close2.1.html=StringIO.StringIO()2.c=pycurl.Curl()3.myurl='http://www.ppgchenshan.com'4. 5.c.setopt(pycurl.URL,myurl)6. 7.#写的回调8.c.setopt(pycurl.WRITEFUNCTION,html.write)9. 10.c.seto
7、pt(pycurl.FOLLOWLOCATION,1)11. 12.#最大重定向次数,可以预防重定向陷阱13.c.setopt(pycurl.MAXREDIRS,5)14. 15.#连接超时设置16.c.setopt(pycurl.CONNECTTIMEOUT,60)17.c.setopt(pycurl.TIMEOUT,300)1. 2.#模拟浏览器3.c.setopt(pycurl.USERAGENT,"Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1;SV1;
8、.NETCLR1.1.4322)")4. 5. 6. 7.#访问,阻塞到访问结束8.c.perform()9. 10.#打印出200(HTTP状态码,可以不需要)11.printc.getinfo(pycurl.HTTP_CODE)12. 13.#输出网页的内容14.printhtml.getvalue()15.#保存成down.txt文件16.writefile(html.getvalue(),"down.txt")python的pycurl模块的安装