spider crawl笔记

1-1     requests库的使用

requests库用来获取网页的源码(source code),response = resquests.get(url),获取后的网页源码用response.text来读取。print(response.text)

requests有get属性和post

payload = {'key1':'value1'}

get用来发送请求 post用来发送有表单的请求,r = requests.post(url,data=payload)

 

1-2  BeautifulSoup

BeautifulSoup是用来格式化获得的网页的html的源码,格式化以后方便后面的读取,有tag,NavigableString,属性class方法

具体用法soup=BeautifulSoup(html,'lxml'),格式化网页。frist_a_title = soup.a#获取第一个a标签

r=soup.find('a')#获取标签为a的内容,只返回第一个找到的结果,soup.find_all(’a‘)#返所有标签为a的结果

print(r)#输出r获取的内容

print(r.name)#输出标签的名字,

print(r.string)#输出标签中的文本内容不包含标签

print(r['class'])#输出标签a的属性值

 

posted on 2017-08-04 14:56  地上的大西瓜  阅读(150)  评论(0编辑  收藏  举报