# 列出http://www.cnblogs.com/xiandedanteng/p/中的标题 from bs4 import BeautifulSoup import requests user_agent='Mozilla/4.0 (compatible;MEIE 5.5;windows NT)' headers={'User-Agent':user_agent} html=requests.get('http://www.cnblogs.com/xiandedanteng/p/',headers=headers) #print(html.text); soup= BeautifulSoup(html.text,'html.parser',from_encoding='utf-8') for titleDiv in soup.find_all(class_="postTitl2"): link=titleDiv.find('a') print(link.string)
输出:html
C:\Users\horn1\Desktop\python\4>python titles.py C:\Users\horn1\AppData\Local\Programs\Python\Python36\lib\site-packages\bs4\__init__.py:146: UserWarning: You provided Unicode markup but also provided a value for from_encoding. Your from_encoding will be ignored. warnings.warn("You provided Unicode markup but also provided a value for from_encoding. Your from_encoding will be ignored.") 如何安装BeautifulSoup4 在过去的二十多年的时间里 linux CentOS6.5 yum安装mysql 5.6(转载&删改) Error: Cannot find module 'express' 之 解决方案 使用Nodejs的Nodemailer经过163信箱发送邮件例程 Nodejs 天涯帖子《鹿鼎记中计》 柳成萌著 下载爬虫 使用js的indexOf,lastIndexOf,slice三函数轻易获得url的服务器,路径和页名 27270图片批量下载爬虫1.00 转帖:心里如果笃定,何惧未知风雨 求边长为一的正方体中,面对角线组成的正四面体体积.
基本达到要求,万里长征又迈出了一小步python