java抓取网页数据_借我一双慧眼吧!网页中抓取关键数据只要三分钟

全文共1589字,预计学习时长10分钟

c00021f52873ecb83638985abe490ed9.png

图源:unsplash

有人说,数据会取代石油的地位,成为未来最珍稀的资源之一。无论这个命题是否成立,毫无疑问,数据或信息(任意形式)已然成为21世纪最宝贵的无形资产之一。

数据极其强大,用途颇广:可以预测销售的未来趋势以获利,可以在医疗保健行业中用于诊断早期结核病,从而挽救患者的生命……而数据科学家要做的是,如何从各种资源中提取有价值的数据。

本文将帮助你掌握这个数据时代的必备技能——如何使用python中的库从网站提取数据。笔者将演示从inshorts网站提取与板球、羽毛球和网球等不同运动有关的新闻报道。

步骤1:导入相关库

import requests             from bs4 importBeautifulSoup             import pandas as pd

步骤2:发出Web请求并使用BeautifulSoup进行解析

先要查看特定新闻类别的源代码。进入网页后将看到不同种类的新闻,关注某一特定的新闻,使用Beautiful Soup提取源代码。在右侧可以看到新闻文章及相应的源代码。

9a5d6e1800fa1f8e13d1984198879aca.png

图源:unsplash

使用请求库,并在URL上使用.get()从网页访问HTML脚本。然后,使用beautiful soup库在python中解析此HTML语言。根据要提取的信息类型,可以使用.find()函数从不同的html标签(例如

, )中过滤该信息。
ee6dbb5d7f85fa93903a289e49eba537.png
dummy_url="https://inshorts.com/en/read/badminton"                                                                  data_dummy=requests.get(dummy_url)                                                                    soup=BeautifulSoup(data_dummy.content,'html.parser')                                                                    soup

完成上述步骤并解析HTML语言后,此特定新闻的部分解析如下所示:

6d2133f1486e9b68992a96da0a1cc645.png

我们看到该文章的标题位于-

类别下,进一步可以看到标题位于 标记中,并且属性为“ itemprop”和“ headline”,可以使用.find()函数进行访问。
news1=soup.find_all('div',class_=["news-card-title news-right-box"])[0]title=news1.find('span',attrs={'itemprop':"headline"}).stringprint(title)We get the following outputgiven below-Shuttler Jayaram wins Dutch OpenGrand Prix

同样,如果要访问新闻内容,则将该新闻设置为

类别。我们还可以看到新闻的正文位于
标记中,该标记的属性为“ itemprop”和“ articleBody”,可以使用.find()函数进行访问。
news1=soup.find_all('div',class_=["news-card-content news-right-box"])[0]content=news1.find('div',attrs={'itemprop':"articleBody"}).stringprint(content)Indian Shuttler Ajay Jayaramclinched $50k Dutch Open Grand Prix at Almere in Netherlands on Sunday,becoming the first Indian to win badminton Grand Prix tournament under a newscoring system. Jayaram defeated Indonesia's Ihsan Maulana Mustofa 10-11, 11-6,11-7, 1-11, 11-9 in an exciting final clash. The 27-year-old returned to thecircuit in August after a seven-month injury layoff.

以类似的方式,我们可以提取图像、作者姓名、时间等任何信息。

步骤3:建立资料集

接下来,我们对3种新闻类别实施此操作,然后将所有文章相应的内容和类别存储在数据框中。笔者将使用三个不同的Urls,对每个URL实施相同的步骤,并将所有文章及其内容设置类别存储为列表形式。

urls=["https://inshorts.com/en/read/cricket","https://inshorts.com/en/read/tennis",     "https://inshorts.com/en/read/badminton"]                                                                      news_data_content,news_data_title,news_data_category=[],[],[]                                                                      for url in urls:                                                                        category=url.split('/')[-1]                                                                      data=requests.get(url) soup=BeautifulSoup(data.content,'html.parser')                                                                        news_title=[]                                                                        news_content=[]                                                                        news_category=[]                                                                        for headline,article inzip(soup.find_all('div', class_=["news-card-titlenews-right-box"]),                                                                                                  soup.find_all('div',class_=["news-card-contentnews-right-box"])):                                                                          news_title.append(headline.find('span',attrs={'itemprop':"headline"}).string)                  news_content.append(article.find('div',attrs={'itemprop':"articleBody"}).string)                                                                          news_category.append(category)                                                                        news_data_title.extend(news_title)                                                                        news_data_content.extend(news_content) news_data_category.extend(news_category)               df1=pd.DataFrame(news_data_title,columns=["Title"])                                                                      df2=pd.DataFrame(news_data_content,columns=["Content"])                                                                      df3=pd.DataFrame(news_data_category,columns=["Category"])                                                                      df=pd.concat([df1,df2,df3],axis=1)                                                                      df.sample(10)

输出为:

9a945a60a7c6c302f8ff30c60e2e4501.png

你可以看到,使用beautiful soup 库在python中抓取网页信息是多么容易,你可以轻松地为任何数据科学项目收集有用数据。从此之后自备“慧眼”,在网页中飞速提取有价值的信息。

43b595b726888a403425dacb6f0d996c.png

留言点赞关注

我们一起分享AI学习与发展的干货

如转载,请后台留言,遵守转载规范

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值