博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
网络爬虫基础练习
阅读量:4447 次
发布时间:2019-06-07

本文共 728 字,大约阅读时间需要 2 分钟。

1.利用requests.get(url)获取网页页面的html文件

import requestsnewsurl='http://news.gzcc.cn/html/xiaoyuanxinwen/'res = requests.get(newsurl) #返回response对象res.encoding='utf-8'

2.利用BeautifulSoup的HTML解析器,生成结构树

from bs4 import BeautifulSoupsoup = BeautifulSoup(res.text,'html.parser')

3.找出特定标签的html元素

soup.p #标签名,返回第一个soup.headsoup.p.name #字符串soup.p. attrs #字典,标签的所有属性soup.p. contents # 列表,所有子标签soup.p.text #字符串soup.p.stringsoup.select(‘li')

5.练习:

取出h1标签的文本

soup.h1.text

取出a标签的链接

a_href = soup.a.attrsprint(a_href)

取出所有li标签的所有内容

l = soup.select('li')

取出一条新闻的标题、链接、发布时间、来源

title = soup.select('.show-title')print(title[0].text)href = soup.select('a')res = soup.select('.show-info')print(res)

 

转载于:https://www.cnblogs.com/orcaleZhang/p/8672687.html

你可能感兴趣的文章
在canvas中使用其他HTML元素
查看>>
React的新特性 ---- Hooks ---- 的基本使用
查看>>
History Introduction to Mining Industry of Czech
查看>>
富文本框
查看>>
动态网页开发基础
查看>>
mysql 恢复备份
查看>>
LeetCode-Create Maximum Number
查看>>
随想之三 -CMDB
查看>>
STM32的DMA
查看>>
Process Class (System.Diagnostics)
查看>>
hdu 3001
查看>>
手机端H5上滑加载下一页
查看>>
Coursera Algorithms week3 快速排序 练习测验: Nuts and bolts
查看>>
Spring框架中Bean管理的常用注解
查看>>
Core Animation系列之CADisplayLink
查看>>
dedecms标签调用大全
查看>>
System.out.print()思考?
查看>>
《与小卡特一起学Python》Code1
查看>>
C#面试题
查看>>
[ZJOI2007]捉迷藏 (点分树+堆*3)
查看>>