好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python网络爬虫功能的基本写法介绍

这篇文章主要介绍了Python网络爬虫功能的基本写法,网络爬虫,即Web Spider,是一个很形象的名字。把互联网比喻成一个蜘蛛网,那么Spider就是在网上爬来爬去的蜘蛛,对网络爬虫感兴趣的朋友可以参考本文

import urllib2
content = urllib2.urlopen('http://XXXX').read() 
import urllib2
from sgmllib import SGMLParser
 
class ListName(SGMLParser):
def init(self):
SGMLParser.init(self)
self.is_h4 = ""
self.name = []
def start_h4(self, attrs):
self.is_h4 = 1
def end_h4(self):
self.is_h4 = ""
def handle_data(self, text):
if self.is_h4 == 1:
self.name.append(text)
 
content = urllib2.urlopen('http://169it.com/xxx.htm').read()
listname = ListName()
listname.feed(content)
for item in listname.name:
print item.decode('gbk').encode('utf8') 
start_tagname(self, attrs)
end_tagname(self) 
from pyquery import PyQuery as pyq
doc=pyq(url=r'http://169it.com/xxx.html')
cts=doc('.market-cat')
 
for i in cts:
print '====',pyq(i).find('h4').text() ,'===='
for j in pyq(i).find('.sub'):
print pyq(j).text() ,
print '\n' 

python爬虫html解析库BeautifulSoup

有个头痛的问题是,大部分的网页都没有完全遵照标准来写,各种莫名其妙的错误令人想要找出那个写网页的人痛打一顿。为了解决这个问题,我们可以选择著名的 BeautifulSoup 来解析html 文档,它具有很好的容错能力。

以上就是本文的全部内容,对Python网络爬虫功能的实现进行了详细的分析介绍,希望对大家的学习有所帮助。

以上就是Python网络爬虫功能的基本写法介绍的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于Python网络爬虫功能的基本写法介绍的详细内容...

  阅读:39次