liubulong 发表于 2019-12-4 14:11:26

python基于BeautifulSoup实现抓取网页

本文实例讲述了python基于BeautifulSoup实现抓取网页指定内容的方法。分享给大家供大家参考。具体实现方法如下:
# -*- coding: utf-8 -*-
import urllib
import sys
from bs4 import BeautifulSoup# 引入美丽鸡汤的模块包
# 把系统编码设置为utf-8
reload(sys)
sys.setdefaultencoding('utf-8')
# 设置初始网页链接, 准备扒下科技论坛的内容
__INITURL__ = "http://aodi.paic.com.cn/forum.php"
# 读取网页,并以html模式解析
soup = BeautifulSoup(urllib.urlopen(__INITURL__),"html.parser")
# 定位到“最新主题”
lvlELements = soup.find('ul', attrs={'class','category_newlist'})
# 把最新主题的标题扒下来
lvl2ELements = lvlELements.find_all('a')
for title in lvl2ELements:
print title.get_text()


页: [1]
查看完整版本: python基于BeautifulSoup实现抓取网页