beautifulsoup 基本语法

#coding=utf-8
import json
import requests
from bs4 import BeautifulSoup
url = 'http://www.itest.info/courses' # 定义被抓取页面的url
soup = BeautifulSoup(requests.get(url).text, 'html.parser')# 获取被抓取页面的html代码,并使用html.parser来实例化BeautifulSoup,属于固定套路
for course in soup.find_all('h4'):# 遍历页面上所有的h4标签
  print course.text.encode('utf-8')# 打印出h4标签的text字符    如: 测试开发--试验班
  print course  # 打印出h4的text字符加标签    如:<h4>测试开发--试验班</h4>

猜你喜欢

转载自www.cnblogs.com/kaibindirver/p/9927297.html