网络爬虫基本练习(代码片段)

118郭耀文 118郭耀文     2022-10-29     773

关键词:

1.取出h1标签的文本

import requests
url = http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0328/9113.html
res = requests.get(url)
res.encoding=utf-8
from bs4 import BeautifulSoup
soup = BeautifulSoup(res.text,html.parser)
soup.h1.text

2.取出a标签的链接

soup.a.attrs.get(href)

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

 for i in soup.select(li):
    print(i.text)

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

soup.select(.news-list-title)[0].text
soup.select(li)[1].a.attrs[href]
soup.select(.news-list-info)[0].contents[0].text
soup.select(.news-list-info)[0].contents[1].text

 

网络爬虫基本练习(代码片段)

1.取出h1标签的文本importrequestsre=requests.get(‘http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0328/9113.html‘)re.encoding=‘utf-8‘print(re)print(re.text)print(soup.h1.text)2.取出a标签的链接soup.a.attrs.get(‘href‘)3. 查看详情

网络爬虫基本练习(代码片段)

<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>SimpleDOMDemo</title></head><body><h1>Thisisthedocumentbody</h1><PID="p1No 查看详情

网络爬虫基础练习(代码片段)

0.可以新建一个用于练习的html文件,在浏览器中打开。1.利用requests.get(url)获取网页页面的html文件importrequestsnewsurl=‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘res=requests.get(newsurl)#返回response对象res.encoding=‘utf-8‘2.利用BeautifulSou 查看详情

网络爬虫基础练习(代码片段)

练习:新建一个用于练习的html文件,在浏览器中打开。<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>SimpleDOMDemo</title></head><body><h1>Thisisthedocumentb 查看详情

网络爬虫基础练习(代码片段)

0.可以新建一个用于练习的html文件,在浏览器中打开。 1.利用requests.get(url)获取网页页面的html文件importrequestsnewsurl=‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘res=requests.get(newsurl)#返回response对象res.encoding=‘utf-8‘ 2.利用 查看详情

网络爬虫基础练习(代码片段)

0.可以新建一个用于练习的html文件,在浏览器中打开。 1.利用requests.get(url)获取网页页面的html文件importrequestsnewsurl=‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘res=requests.get(newsurl)#返回response对象res.encoding=‘utf-8‘ 2.利用 查看详情

网络爬虫基础练习(代码片段)

0.可以新建一个用于练习的html文件,在浏览器中打开。 1.利用requests.get(url)获取网页页面的html文件importrequestsnewsurl=‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘res=requests.get(newsurl)#返回response对象res.encoding=‘utf-8‘ 2.利用 查看详情

网络爬虫基础练习(代码片段)

0.可以新建一个用于练习的html文件,在浏览器中打开。 1.利用requests.get(url)获取网页页面的html文件importrequestsnewsurl=‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘res=requests.get(newsurl)#返回response对象res.encoding=‘utf-8‘ 2.利用 查看详情

网络爬虫基础练习(代码片段)

0.可以新建一个用于练习的html文件,在浏览器中打开。 1.利用requests.get(url)获取网页页面的html文件importrequestsnewsurl=‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘res=requests.get(newsurl)#返回response对象res.encoding=‘utf-8‘ 2.利用 查看详情

网络爬虫基础练习(代码片段)

0.可以新建一个用于练习的html文件,在浏览器中打开。 1.利用requests.get(url)获取网页页面的html文件importrequestsnewsurl=‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘res=requests.get(newsurl)#返回response对象res.encoding=‘utf-8‘ 2.利用 查看详情

网络爬虫基础练习(代码片段)

0.可以新建一个用于练习的html文件,在浏览器中打开。 1.利用requests.get(url)获取网页页面的html文件importrequestsnewsurl=‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘res=requests.get(newsurl)#返回response对象res.encoding=‘utf-8‘ 2.利用 查看详情

网络爬虫基础练习(代码片段)

0.可以新建一个用于练习的html文件,在浏览器中打开。 1.利用requests.get(url)获取网页页面的html文件importrequestsnewsurl=‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘res=requests.get(newsurl)#返回response对象res.encoding=‘utf-8‘ 2.利用 查看详情

网络爬虫基础练习(代码片段)

0.可以新建一个用于练习的html文件,在浏览器中打开。 1.利用requests.get(url)获取网页页面的html文件importrequestsnewsurl=‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘res=requests.get(newsurl)#返回response对象res.encoding=‘utf-8‘ 2.利用 查看详情

网络爬虫基础练习(代码片段)

0.可以新建一个用于练习的html文件,在浏览器中打开。 1.利用requests.get(url)获取网页页面的html文件importrequests#发送请求url="http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0328/9113.html"res=requests.get(url)res.encoding="utf-8"res.tex 查看详情

网络爬虫基础练习(代码片段)

1.利用requests.get(url)获取网页页面的html文件importrequestsnewsurl=‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘res=requests.get(newsurl)#返回response对象res.encoding=‘utf-8‘ 2.利用BeautifulSoup的HTML解析器,生成结构树frombs4 查看详情

网络爬虫基础练习(代码片段)

0.可以新建一个用于练习的html文件,在浏览器中打开。上课时老师所给的html文件,便于数据爬虫。 <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>GZCC新闻网</title></head><body><h 查看详情

网络爬虫基础练习(代码片段)

1.利用requests.get(url)获取网页页面的html文件importrequestsnewsurl=‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘res=requests.get(newsurl)#返回response对象res.encoding=‘utf-8‘2.利用BeautifulSoup的HTML解析器,生成结构树frombs4import 查看详情

网络爬虫基础练习(代码片段)

可以新建一个用于练习的html文件,在浏览器中打开。 1.利用requests.get(url)获取网页页面的html文件importrequestsurl="http://localhost:63342/bd/gzcc.html?_ijt=1s9ucf4k9asfpkjs4lhl0cg6vp"res=requests.get(url)res.encoding=‘utf-8‘res.te 查看详情