cmdpython python anocandapython Beautiful Souppip install Beautiful Soup
爬取前的准� • 打开cmd窗口,进入python安装目录 下载python,配置环境(可 使用anocanda,里面提供了很多python模块) • • Beautiful. Soup的导入:pip install Beautiful. Soup 4 requests的导入:pip install requests pandas的导入:pip install pandas 下载jupyter notebook(可选):pip install jupyter notebook
requests示例 • 下面是在python中使用requests包中get方法的小例子 #requests. get示例 import requests res=requests. get('http: //news. sina. com. cn/china/') res. encoding='utf-8' #这一句是为了避免中文乱码 print(res) #输出结果是<Response [200]>,可知resquests. get返回回复的 数量,而不是回复的内容 print(res. text) #因此加上“. text”才是得到网页内容
requests示例 • 输出结果的前面一小部分显示如下。可以发现,requests. get获取的内 容是一个完整的HTML文档,不仅包括网页显示的信 息,还包括了 html的标签。为了将这些标签去掉,就需要使用到Beautiful. Soup 4。 <Response [200]> <!DOCTYPE html> <!-- [ published at 2018 -08 -09 19: 30: 48 ] --> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <title>国内新� _新�中心 _新浪网</title> <meta name="keywords" content="国内�政 , 内地新� "> <meta name="description" content="新�中心国内�道,��国内�政、�述��及 �片的�目,主要包括 �政要�、内地新�、港澳台新�、媒体聚焦、��分析。 "> <meta name="robots" content="noarchive"> <meta name="Baiduspider" content="noarchive"> <meta http-equiv="Cache-Control" content="no-transform"> <meta http-equiv="Cache-Control" content="no-siteapp"> <meta name="applicable-device" content="pc, mobile"> <meta name="Mobile. Optimized" content="width"> <meta name="Handheld. Friendly" content="true">
��的 Beautiful. Soup示例 #简单的Beautiful. Soup示例 from bs 4 import Beautiful. Soup html_sample=' <html> <body> <h 1 id="title">hello world</h 1> <a href="#" class="link">This is link 1</a> <a href="# link 2" class="link">This is link 2</a> </body> </html>' soup=Beautiful. Soup(html_sample, 'html. parser')#指定解析器html. parser print(soup) #没有去掉标签 print(soup. text) #把里面的内容截取出来,而去掉标签
示例:使用select找出含有h 1标签的元素 #使用select找出含有h 1标签的元素 from bs 4 import Beautiful. Soup html_sample=' <html> <body> <h 1 id="title">hello world</h 1> <a href="#" class="link">This is link 1</a> <a href="# link 2" class="link">This is link 2</a> </body> </html>' soup = Beautiful. Soup(html_sample, 'html. parser') header = soup. select('h 1') print(header)#回传Python的一个list print(header[0])#解开这个回传的list,打[0]时没有两边的中括号 print(header[0]. text)#只获取里面的文字
示例:使用select找出含有a标签的元素 #使用select找出含有a标签的元素 soup = Beautiful. Soup(html_sample, 'html. parser') alink = soup. select('a') print(alink) for link in alink: print(link) print(link. text)
示例:使用select找出所有id为title的元素 #使用select找出所有id为title的元素 soup = Beautiful. Soup(html_sample, 'html. parser') alink = soup. select('#title') #(id前面需加上#) print(alink)
示例:获取a标签中的不同属性值 #获取a标签中的不同属性值 a='<a href="#" qoo=123 abc=456> i am a link </a>' soup 2=Beautiful. Soup(a, 'html. parser') #print(link['href']) print(soup 2. select('a')[0]) print(soup 2. select('a')[0]['href']) #最后一个中括号里面可以是 'abc', 'qoo', 'href',放入不同的属性名称,就可以取得对应的属性值
将requests与 Beautiful. Soup结合使用的 一些例子
示例:新浪新闻主页信息获取 #新浪新闻主页信息获取 import requests from bs 4 import Beautiful. Soup res =requests. get('https: //news. sina. com. cn/china/') #获取新浪新闻国内主页的全部信息 res. encoding='utf-8' soup=Beautiful. Soup(res. text, 'html. parser') #print(len(res. text)) ###仅提取新闻标题、来源的全部列表 for news in soup. select('. right-content'): alink=news. select('a') for link in alink: tl = link. text a = link['href'] #txt = news. select('. txt')[0]. text print(tl, a)#打印标题和链接
示例:获取某一篇文章的标题、日期、来 源、正文等内容 #获取某一篇文章的标题、日期、来源、正文等内容 import requests from bs 4 import Beautiful. Soup res =requests. get('http: //news. sina. com. cn/o/2018 -03 -16/doc-ifysiesm 9100707. shtml') res. encoding='utf-8' #print(res. text) #这里得到的是带有html标签的网页内容 soup=Beautiful. Soup(res. text, 'html. parser') #将requests. get获取得到的res对象变成 Beautiful. Soup对象,供后面的每个字段属性的提取 title=soup. select('. main-title')[0]. text #获取标题 datesource=soup. select('. date')[0]. text #获取日期 source=soup. select('. source')[0]. text #获取来源 sourcelink=soup. select('. source')[0]['href'] #获取来源链接 article=soup. select('. article')[0]. text #获取正文内容 print(title, datesource, sourcelink, article)
示例:输出字符串型的date和时间型的 date #输出字符串型的date和时间型的date from datetime import datetime datesource = "2019年 4月26日 16: 47" dt=datetime. strptime(datesource, '%Y年%m月%d日 %H: %M') print(dt) type(dt)
示例:对新闻正文内容的抓取 import requests from bs 4 import Beautiful. Soup res =requests. get('http: //news. sina. com. cn/gov/2017 -11 -02/docifynmzrs 6000226. shtml') res. encoding='utf-8' soup=Beautiful. Soup(res. text, 'html. parser') title=soup. select('#artibody')[0]. text print(title)
- Slides: 18