python 爬虫之BeautifulSoup 库的基本使用

import urllib2
url = 'http://www.someserver.com/cgi-bin/register.cgi'
values = {}
values['name'] = 'Michael Foord'
values['location'] = 'Northampton'
values['language'] = 'Python'

data = urllib.urlencode(values) #数据进行编码生成get方式的请求字段
req = urllib2.Request(url,data) #作为data参数传递到Request对象中 POST方式访问
response = urllib2.urlopen(req) 返回一个类文件对象
the_page = response.read()
soup = BeautifulSoup(the_page,"html.parser") 通过类文件the_page 创建beautifulsoup对象,soup的内容就是页面的源码内容
构造好BeautifulSoup对象后,借助find()和find_all()这两个函数,可以通过标签的不同属性轻松地把繁多的html内容过滤为你所想要的
url_name = line.get('href') 获取a标签的url信息
Title = line.get_text().strip() 获取a标签的文本内容

猜你喜欢

转载自blog.51cto.com/weadyweady/2307779