Lisy's blog Lisy's blog
首页
后端
前端
读书
关于

新晴

做事情积极主动。积极主动不仅指行事的态度,还意味着为人一定要对自己的人生负责。个人行为取决于自身的抉择,而不是外在的环境,人类应该有营造有利的外在环境的积极性和责任感。
首页
后端
前端
读书
关于
  • Java

  • linux

  • python

    • python开发学习
    • 利用requests跟BeautifulSoup获取网页内容
  • 其他

  • python

  • 后端
  • python
lisy
2023-09-17

利用requests跟BeautifulSoup获取网页内容

对于下面这个形式的html,java中用jsoup来处理,python里面可以用requests+BeautifulSoup来处理

html源码

<html>
    <header>
        <title>测试</title>
    </header>
    <body>
        <div class="post-preview">
            <a href="https://127.0.0.1:90/archives/rrrr">
                <h2 class="post-title">
                    标题1
                </h2>
                <div class="post-content-preview">
                    摘要1
                </div>
            </a>
            <p class="post-meta">
                信息1
            </p>
        </div>
        <hr>
        <div class="post-preview">
            <a href="https://127.0.0.1:90/archives/rrrr4">
                <h2 class="post-title">
                    标题4
                </h2>
                <div class="post-content-preview">
                    摘要4
                </div>
            </a>
            <p class="post-meta">
                信息4
            </p>
        </div>
        <hr>
        <div class="post-preview">
            <a href="https://127.0.0.1:90/archives/rrrr2">
                <h2 class="post-title">
                    标题2
                </h2>
                <div class="post-content-preview">
                    摘要2
                </div>
            </a>
            <p class="post-meta">
                信息2
            </p>
        </div>
        <hr>
        <div class="post-preview">
            <a href="https://127.0.0.1:90/archives/rrrr3">
                <h2 class="post-title">
                    标题3
                </h2>
                <div class="post-content-preview">
                    摘要3
                </div>
            </a>
            <p class="post-meta">
                信息3
            </p>
        </div>
        <hr>
        <ul class="pager">
            <li class="next">
                <a href="https://127.0.0.1:90/page/2">Older Posts →</a>
            </li>
        </ul>
    </body>
</html>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

python 代码

import requests
from bs4 import BeautifulSoup

links = []
index = 1
isEnd = False

while not isEnd:
    url = f"http://127.0.0.1:90/page/{index}"
    response = requests.get(url, timeout=30)
    soup = BeautifulSoup(response.text, "html.parser")
    content = soup.select(".post-preview a")

    for link in content:
        linkHref = link["href"]
        links.append(linkHref)

    contentEnd = soup.select(".next a")
    if len(contentEnd) == 0:
        isEnd = True

    index += 1

print(join(links))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
上次更新: 2024/01/07, 07:44:52
python开发学习
为知笔记记录了我的职业生涯

← python开发学习 为知笔记记录了我的职业生涯→

最近更新
01
Anaconda conda命令速查
01-05
02
奇特的一生
06-16
03
docker学习
02-16
更多文章>
Theme by Vdoing | Copyright © 2020-2025 lisy | 备案号:鲁ICP备18023702号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式