Web scraper使用教程-进阶用法(二)-爬取二级页面内容

进阶用法(二)-爬取二级页面内容

1. 爬取网址

https://docs.microsoft.com/en-us/officeupdates/update-history-microsoft365-apps-by-date

爬取下表链接的二级页面

2. 新建项目updatehistory

Create new sitemap->create sitemap

Add new selector

Id :currentchannel

Type : link

Selector

multiple:勾选

Save

3. 点击ID为currentchannel的selector

4. 随便点击一个link,进入它的二级页面

5. 准备爬取的内容为右边的链接

6. Add new selector

Id :versionlink

Type :link

Selector

Multiple :勾选

 

7. 保存后,可查看一下,data preview

 

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是关于使用 Python 的 web scraper 爬取二级网页的简单教程: 1. 导入必要的库 在 Python 中,我们需要使用一些第三方库来实现网页爬取。其中,最为常用的包括 requests 和 BeautifulSoup。前者用于获取网页源代码,后者用于解析 HTML 代码。 ```python import requests from bs4 import BeautifulSoup ``` 2. 获取二级网页 要获取二级网页的内容,我们首先需要获取主网页的链接,然后使用 requests 库获取其源代码,进而找到所有二级网页的链接。我们可以使用 BeautifulSoup 库解析 HTML 代码,并使用 find_all 方法找到所有 a 标签中的 href 属性,即所有链接。 ```python # 获取主网页的链接 url = "https://example.com" response = requests.get(url) soup = BeautifulSoup(response.content, "html.parser") # 获取所有二级网页的链接 links = [] for link in soup.find_all("a"): href = link.get("href") if href.startswith("https://example.com/second-level"): links.append(href) ``` 这里我们假设所有二级链接都以 "https://example.com/second-level" 开头。 3. 爬取二级网页的内容 现在我们已经得到了所有二级网页的链接,接下来就可以依次爬取它们的内容了。和获取主网页的源代码类似,我们可以使用 requests 库获取每个二级网页的源代码,然后使用 BeautifulSoup 库解析 HTML 代码,找到我们需要的内容。 ```python # 爬取所有二级网页的内容 for link in links: response = requests.get(link) soup = BeautifulSoup(response.content, "html.parser") # 这里可以根据需要找到需要的内容 ... ``` 这里需要注意的是,每个二级网页的 HTML 代码可能会有所不同。因此,我们需要根据实际情况找到我们需要的内容。 4. 完整代码 以下是一个完整的示例程序,用于爬取某个网站中以 "https://example.com/second-level" 开头的所有二级网页的标题和正文: ```python import requests from bs4 import BeautifulSoup # 获取主网页的链接 url = "https://example.com" response = requests.get(url) soup = BeautifulSoup(response.content, "html.parser") # 获取所有二级网页的链接 links = [] for link in soup.find_all("a"): href = link.get("href") if href.startswith("https://example.com/second-level"): links.append(href) # 爬取所有二级网页的内容 for link in links: response = requests.get(link) soup = BeautifulSoup(response.content, "html.parser") # 找到标题和正文 title = soup.find("h1").text content = soup.find("div", class_="content").text # 输出结果 print("Title:", title) print("Content:", content) ``` 这段代码可以根据实际需要进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值