爬虫入门——requests和Beautifulsoup

操作环境:python3

首先,使用requests库获取HTTP请求

#HTTP请求类型
#get类型
r = requests.get('ulr')
#post类型
r = requests.post("url")
#put类型
r = requests.put("url")
# 因为到目前为止只使用了这三种请求类型,所以先列出这三种类型用法
# 获取返回页面
response = requests.get(url='地址')
# 使返回页面的编码格式和原页面相同
response.enconding = apparent_encoding
# 返回文本页面信息
response.text

  

  获取到返回的信息后,再根据页面上的内容去爬取自己所需要的信息,GET方法和POST方法居多。

  然后使用Beautifulsoup解析获取到的页面,再获取到想得到的内容。

 # 2、解析,获取想要的页面内容,格式为 bs4.BeautifulSoup
  soup = BeautifulSoup(ret.text,'html.parser') # lxml   print(type(soup))  # 只有对象有find功能   div = soup.find(name='div', id='auto-channel-lazyload-article') li_list = div.find_all(name='li')

  

  

猜你喜欢

转载自www.cnblogs.com/jieranfeng/p/9269165.html