Web搜寻网站的多个网页

2024-02-26 11:50
文章标签 网页 网站 web 多个 搜寻

本文主要是介绍Web搜寻网站的多个网页,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

“Getting information off the internet is like taking a drink from a fire hose.” — Mitchell Kapor

“从互联网上获取信息就像从消防水带上喝一杯。” —米切尔·卡波(Mitchell Kapor)

A data scientist must go through the pain of finding data. But just finding the data isn’t enough, there are changes that must be made to the data so that it can be used for practical purposes. But is data easily available?

数据科学家必须经历寻找数据的痛苦。 但是仅仅找到数据是不够的,必须对数据进行更改,以便将其用于实际目的。 但是数据容易获得吗?

不,不是。 (No,it isn’t.)

In this article, i will show you how challenging it can be to gather data. We will also learn how to deal with such problems.

在本文中,我将向您展示收集数据的挑战 。 我们还将学习如何处理此类问题。

Before we jump to the problem, let us first understand the difference between a static website and a dynamic website. You might have noticed while going through your twitter feed as you scroll down, the tweets are loaded without the webpage being reloaded. How does this happen?

在跳到问题之前,让我们首先了解静态网站和动态网站之间的区别。 向下滚动浏览Twitter提要时,您可能已经注意到,加载了这些推文而没有重新加载该网页。 这是怎么发生的?

This happens because Twitter is a dynamic website. Dynamic website uses client-side scripting or server-side scripting, or both to generate dynamic content. To know more click here

发生这种情况是因为Twitter是一个动态网站。 动态网站使用客户端脚本或服务器端脚本,或同时使用两者来生成动态内容。 要了解更多,请点击这里

Image for post
Static V/s Dynamic Webpage
静态V / s动态网页

问题: (Problem :)

CodingBat is a website of live coding problems to build coding skill in Java and Python. CodingBat is a static website, which means it is easier to scrape data from but the challenge is different here!!

CodingBat是一个实时编码问题的网站,旨在培养Java和Python的编码技能。 CodingBat是一个静态网站,这意味着从中抓取数据更容易,但是这里的挑战不一样!!

Data Required: For all the coding enthusiasts, I wish to scrape all the java questions from all the sub-topics on the website.

所需数据 :对于所有编码爱好者,我希望从网站上所有子主题中删除所有Java问题。

Let’s see how the website looks like.

让我们看看网站的外观。

Image for post
Many Subtopics
许多副题

As you can see in the above picture, Java section has many subtopics, so lets click on the first one and see where this takes us.Hopefully to questions :)

如上图所示,Java部分包含许多子主题,因此让我们单击第一个子主题,看看它将我们带到何处。

Image for post
Set of questions for a subtopic
副题集

So we land up in a page of a another set of topics !! Let us see where the first link “sleepIn” takes us.

因此,我们进入了另一组主题的页面! 让我们看看第一个链接“ sleepIn”将我们带到何处。

Image for post
Question to be scraped
待刮问题

So finally we arrive on the webpage which has a question. But this is just one question. What can we do to get other java related questions? Should we scrape them one by one ? That’s a tedious task. Web Scraping is meant to collect huge amounts of data at once and make our life easier. But we don't see that happening here. Don’t loose hope !!

所以最后我们到达了一个有疑问的网页。 但这只是一个问题。 我们如何做才能得到其他与Java相关的问题? 我们应该一头一刮吗? 那是一个乏味的任务。 Web Scraping旨在一次收集大量数据,并使我们的生活更轻松。 但是我们看不到这种情况发生。 不要失去希望!

挑战: (The challenge :)

Every time we wish to scrape data from a website, it is not necessary that we get the data in a single web page, like in a Wikipedia table. We might have to hop to different links on the website only to find that the data we need is present only in a small amount, which is useless.

每当我们希望从网站上抓取数据时,就不必像在Wikipedia表中那样在单个网页中获得数据。 我们可能不得不跳到网站上的其他链接,只是发现我们所需的数据仅少量存在,这是无用的。

Please understand that the above example was chosen only to highlight one of many problems that you encounter while web scraping. But, it must be tackled.

请理解,选择上面的示例只是为了突出您在网络抓取时遇到的许多问题之一。 但是,必须解决它。

解决方案 : (The solution :)

You need to have some basic knowledge of HTML pages to understand web scraping. We also need some python libraries like BeautifulSoup & Requests .

您需要具备HTML页面的一些基本知识才能了解网络抓取。 我们还需要一些Python库,例如BeautifulSoup 请求

Let’s now understand the steps involved to scrape data from multiple webpages on a website.

现在,让我们了解从网站上的多个网页抓取数据所涉及的步骤。

  1. Install BeautifulSoup and import necessary libraries : pip install beautifulsoup4 (Go to the terminal and use this pip command to install it)

    安装BeautifulSoup并导入必要的库 :pip install beautifulsoup4(转到终端并使用此pip命令进行安装)

  2. Save the HTML content of the website in a text file : When scraping a static website, saving its html content in a file is better as you can request for a page only a limited number of times. Once you have the HTML code saved in a text file, it can be used infinitely for scraping instead of making a call to the website multiple times.

    将网站HTML内容保存在文本文件中 :抓取静态网站时,将其html内容保存在文件中会更好,因为您可以只请求有限次数的页面。 将HTML代码保存在文本文件中后,就可以将其无限次用于抓取,而不必多次致电网站。

# import libraries
from bs4 import BeautifulSoup
import requestsurl = 'https://codingbat.com/java'
file_name = 'pagesource.txt' #with keyword- automatically closes the file
page = requests.get(url)
with open(file_name,'w') as file:
file.write(page.content.decode('utf-8')) if type(page.content) == bytes else file.write(page.content)

This is how the text files looks like :

这是文本文件的外观:

Image for post
HTML Code saved in a text file
HTML代码保存在文本文件中

3. Define a function to read the text file :

3. 定义一个读取文本文件的函数:

#define a fucntion to read a file
def read_file():
file = open('pagesource.txt')
data = file.read()
file.close()
return data

4. Extract links to all subtopics in Java section using BeautifulSoup : Let’s “Inspect” the CodingBat website to find where the links to subtopics are present.

4. 使用BeautifulSoup提取到Java部分中所有子主题的链接 :让我们“检查” CodingBat网站以找到子主题链接的位置。

Image for post
Using Inspect to locate <a> tags
使用Inspect定位<a>标签

It is clear from the above figure that the link is present inside the <div> tag whose class attribute value is “summ”. Also notice the href attribute of the <a> tag. Its a partial link. So we need to concatenate the base URL of the website with this href value so as to form the proper link.

从上图可以清楚地看出,链接存在于<div>标记内,其类别属性值为“ summ”。 还要注意<a>标记的href属性。 它是部分链接。 因此,我们需要使用该href值连接网站的基本URL,以形成正确的链接。

soup = BeautifulSoup(read_file(), 'html.parser')
base_url = 'https://codingbat.com'
urls = soup.find_all('div', class_='summ')# Extracting the href attribute of "<a>" which is the first child of the <div> tag
main_links = [div.a['href'] for div in urls]# Append links in a list
new_links = []
for links in main_links:
out_links = base_url + links
new_links.append(out_links)
print(*new_links,sep="\n")

5. Extracting sub-links : Now we have the links to each subtopic, but each sub-topic further contains the links to several questions. So the next task is to extract the links to each and every question belonging to each sub-topic.But first lets see where the link to each question is stored in the HTML content.

5. 提取子链接 :现在我们有每个子主题的链接,但是每个子主题还包含一些问题的链接。 因此,下一个任务是提取与每个子主题相关的每个问题的链接,但是首先让我们看看每个问题的链接在HTML内容中的存储位置。

Image for post
Links to questions present inside <td> tag
链接到<td>标记内的问题
#PART-2-EXTRACTING SUB-LINKS    
main_firstlinks = []# for the links to questions in each sub-topic
for i in new_links:
curr_link = i
#print(curr_link, "\n")
response = requests.get(curr_link)
html = response.text
soup = BeautifulSoup(html, 'lxml')
first_links = soup.find_all('td', width="200")
for mfl in first_links:
half_links = mfl.a['href']
out_links1 = base_url + half_links
main_firstlinks.append(out_links1)

print(*main_firstlinks, sep="\n")

6. Extracting questions : Finally we attempt to scrape all the questions from all the links. Let’s see where the question is stored in the HTML content.

6. 提取问题 :最后,我们尝试从所有链接中抓取所有问题。 让我们看看问题在HTML内容中的存储位置。

Image for post
Question has 2 parts
问题共2部分

Observe that there are two parts of the question, the statement and the functions. The statement part is stored in table->div->p.

注意问题有两个部分,语句和函数。 语句部分存储在table-> div-> p中。

The function part is a sibling of the <div> tag . To know how we can navigate sideways in BeautifulSoup refer to the BeautifulSoup Documentation

功能部分是<div>标签的同级元素。 要了解我们如何在BeautifulSoup中横向浏览,请参阅BeautifulSoup文档

#PART -3- EXTRACTING ALL QUESTIONS#
for k in main_firstlinks:
# new_link=main_firstlinks[k]
new_response = requests.get(k)
new_data = new_response.text
soup = BeautifulSoup(new_data, 'lxml')
p = soup.find('div', class_="indent")
para_text = p.table.div.string
# print(para_text)
print("Question: ", para_text) rest = p.table.div.next_siblings
for sibling in rest:
if sibling.string is not None:
print(sibling.string)
print("\n\n")

Output :

输出

Image for post
All questions scraped from CodingBat Java Section
所有问题均从CodingBat Java部分刮掉

This is only a sample of four questions from the output obtained. So you see how challenging it can be to collect data ? However, now you know what to do when you face a similar situation while scraping data from a static website.

这只是从输出中获得的四个问题的样本。 因此,您知道收集数据有多么艰巨吗? 但是,现在您知道在从静态网站抓取数据时遇到类似情况时该怎么办。

Refer to my GitHub Code

请参阅我的GitHub代码

Note : All the resources that you will require to get started have been mentioned and their links provided in this article as well. I hope you make good use of it :)

注意 :已经提到了您将需要的所有资源,本文还提供了它们的链接。 我希望你能好好利用它:)

I hope this article will get you interested in trying out new things like web scraping and help you add to your knowledge. Don’t forget to click on the “clap” icon below if you have enjoyed reading this article. Thank you for your time.

希望本文能使您对尝试新功能(例如网络抓取)感兴趣,并帮助您增加知识。 如果您喜欢阅读本文,请不要忘记单击下面的“拍手”图标。 感谢您的时间。

翻译自: https://medium.com/analytics-vidhya/web-scraping-multiple-webpages-of-a-website-1354040b7cba


http://www.taodudu.cc/news/show-8472582.html

相关文章:

  • mac中在html中引入react.development.js、react-dom.development.js报错
  • LINK:fatal error LNK1104 无法打开文件“.exe”
  • react精华之服务端渲染 ReactDOMServer 16版本后renderToString 转化成renderToNodeStream的好处 React v16 之后用 React.hydr
  • 论文阅读 (57):2-hydr_Ensemble: Lysine 2-Hydroxyisobutyrylation Identification with Ensemble Method (任务)
  • fatal error LNK1104: cannot open file ‘release/hydr3d‘
  • HTML写页面,实现两个日期的相减
  • word录入表单数据 java 导入系统,PoiDemo【Android将表单数据生成Word文档的方案之二(基于Poi4.0.0)】...
  • 测试面试题常见题
  • 「好文档自己会说话」春节倒休计划表
  • git在push时报错:! [rejected] master -> master (non-fast-forward) error: failed to push some refs to
  • org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): cn.qdm.srm.web.sys.
  • 达梦数据库-linux环境下断网后连接卡住问题解决
  • 上传代码到gitlab及下载代码
  • 关于安装tqdm==4.62.3版本包遇到的一个问题学到的一个tips
  • BcUnit 交叉编译
  • 数据库建表权限 CREATE command denied to user for table
  • 达梦数据库QOCI驱动编译
  • Qt应用程序连接达梦数据库-飞腾PC麒麟V10
  • DM接口编程
  • 【数据库】Windows + QT + 达梦数据库部署
  • pip install出现WARNING: Ignoring invalid distribution的解决方案
  • QDM FOR SQL MODEL
  • java定时定量提交数据库_request对象 - osc_f5e60qdm的个人空间 - OSCHINA - 中文开源技术交流社区...
  • 数据库周刊65丨TiDB 5.0 GA版发布;达梦与沃趣打造数据库一体机“QDM”;Oracle日志切换频繁问题;MySQL连表查询优化案例;PG SQL开发规
  • Linux环境中使用QT5通过QDM连接达梦数据库
  • 【气候模式降尺度】分位数增量映射(QDM)原理及MATLAB代码实现
  • windows平台编译达梦数据库qdm驱动
  • 亚马逊鲲鹏系统一款令人瞩目的全能软件
  • 大模型时代如何拥抱原生AI?“云智一体”千帆改变AI格局
  • 大模型机器人发展史:从VoxPoser、RT2到斯坦福Mobile ALOHA、Google机器人
  • 这篇关于Web搜寻网站的多个网页的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



    http://www.chinasem.cn/article/748792

    相关文章

    问题-windows-VPN不正确关闭导致网页打不开

    为什么会发生这类事情呢? 主要原因是关机之前vpn没有关掉导致的。 至于为什么没关掉vpn会导致网页打不开,我猜测是因为vpn建立的链接没被更改。 正确关掉vpn的时候,会把ip链接断掉,如果你不正确关掉,ip链接没有断掉,此时你vpn又是没启动的,没有域名解析,所以就打不开网站。 你可以在打不开网页的时候,把vpn打开,你会发现网络又可以登录了。 方法一 注意:方法一虽然方便,但是可能会有

    JavaWeb系列二十: jQuery的DOM操作 下

    jQuery的DOM操作 CSS-DOM操作多选框案例页面加载完毕触发方法作业布置jQuery获取选中复选框的值jQuery控制checkbox被选中jQuery控制(全选/全不选/反选)jQuery动态添加删除用户 CSS-DOM操作 获取和设置元素的样式属性: css()获取和设置元素透明度: opacity属性获取和设置元素高度, 宽度: height(), widt

    相关网站

    力扣  https://leetcode-cn.com/contest/weekly-contest-124

    JavaWeb系列六: 动态WEB开发核心(Servlet) 上

    韩老师学生 官网文档为什么会出现Servlet什么是ServletServlet在JavaWeb项目位置Servlet基本使用Servlet开发方式说明快速入门- 手动开发 servlet浏览器请求Servlet UML分析Servlet生命周期GET和POST请求分发处理通过继承HttpServlet开发ServletIDEA配置ServletServlet注意事项和细节 Servlet注

    如何实现一台机器上运行多个MySQL实例?

    在一台机器上一个MySQL服务器运行多个MySQL实例有什么好处?这里我先入为主给大家介绍这样做至少存在两个好处(看完这篇文章后理解会更透彻): (1)减轻服务器链接负担 (2)为不同的用户提供不同的mysqld服务器的访问权限以方便这些用户进行自我管理。   下面我介绍具体的实现过程: 一、准备工作     台式机一台、Windows系统、MySQL服务器(我安装的版本是MySQL

    HTML5文旅文化旅游网站模板源码

    文章目录 1.设计来源文旅宣传1.1 登录界面演示1.2 注册界面演示1.3 首页界面演示1.4 文旅之行界面演示1.5 文旅之行文章内容界面演示1.6 关于我们界面演示1.7 文旅博客界面演示1.8 文旅博客文章内容界面演示1.9 联系我们界面演示 2.效果和源码2.1 动态效果2.2 源代码2.3 源码目录 源码下载万套模板,程序开发,在线开发,在线沟通 作者:xcLeigh

    JavaWeb 学习笔记 spring+jdbc整合开发初步

    JdbcTemplate类是Spring的核心类之一,可以在org.springframework.jdbc.core中找到它。JdbcTemplate类在内部已经处理数据库的建立和释放,可以避免一些常见的错误。JdbcTemplate类可直接通过数据源的应用实例化,然后在服务中使用,也可在xml配置中作为JavaBean应用给服务使用直接上一个实例步骤1.xml配置 <?xml version

    网页脚本输入这么简单

    如何在网页中进行脚本操作呢? 研究了一下,很简单,用google浏览器的Console直接操作javaScript。思路: Created with Raphaël 2.1.0 开始 输入(如何输入) 点击(如何点击) 结束 下面是,通过脚本刷直播屏的实现,直接在Console输入即可 var words=new Arra

    大型网站架构演化(六)——使用反向代理和CDN加速网站响应

    随着网站业务不断发展,用户规模越来越大,由于中国复杂的网络环境,不同地区的用户访问网站时,速度差别也极大。有研究表明,网站访问延迟和用户流失率正相关,网站访问越慢,用户越容易失去耐心而离开。为了提供更好的用户体验,留住用户,网站需要加速网站访问速度。      主要手段:使用CDN和反向代理。如图。     使用CDN和反向代理的目的都是尽早返回数据给用户,一方面加快用户访问速

    大型网站架构演化(五)——数据库读写分离

    网站在使用缓存后,使绝大部分数据读操作访问都可以不通过数据库就能完成,但是仍有一部分读操作(缓存访问不命中、缓存过期)和全部的写操作需要访问数据库,在网站的用户达到一定规模后,数据库因为负载压力过大而成为网站的瓶颈。      目前豆粉的主流数据库都提供主从热备功能,通过配置两台数据库主从关系,可以将一台数据库服务器的数据更新同步到另一台服务器上。网站利用数据库的这一功能,