一、库的导入
from urllib.request import urlopen
from bs4 import BeautifulSoup
from urllib import parse
import requests
二、新闻标题的爬取
html = urlopen("http://xgxy.hbue.edu.cn/")#打开所需爬取的页面
bs = BeautifulSoup(html,'html.parser')#用BeautifulSoup解析网页
p1 = bs.findAll('div',{'class':'news_tit'})#找到新闻标题的所在标签名称
for each in p1:
titles = each.select('a')[0]['title']#即a标签下的title
print(titles)


三、爬取新闻的内容
page_url = "http://xgxy.hbue.edu.cn/"
news_full_urls = []
p1 = bs.findAll('div',{'class':'news_tit'})
#爬取新闻内容的链接
for each

本文介绍了如何结合requests和BeautifulSoup库爬取新闻标题和内容,详细讲解了库的导入、新闻标题的抓取以及内容的获取。还提及了parse.urljoin在构建绝对URL中的作用,以及BeautifulSoup的findAll和find方法的使用。另外,提到了selenium库作为另一种爬虫选择,它可以实现网页动态加载和页面截屏,但需要与其他浏览器配合使用。
最低0.47元/天 解锁文章
1120

被折叠的 条评论
为什么被折叠?



