python - 使用 Python 和 BeautifulSoup 抓取多个页面

标签 python beautifulsoup

我尝试使用 BeautifulSoup 在 Python 中抓取许多页面,但没有得到积极的结果。

我尝试使用request.get()session.get()。我应该抓取的页数是 92。

import requests
from bs4 import BeautifulSoup
import urllib.request
with requests.Session as session:
    count = 0
    for i in range(92):
        count+=1
        page = "https://www.paginegialle.it/lazio/roma/dentisti/p-"+str(count)+".html"
        r = session.get(page)
        soup = BeautifulSoup(r.content)

使用print(page)页面的格式正确。但是执行soup来打印变量中存储的所有值,只打印第一页的值。 我正在使用 jupyter 笔记本

最佳答案

您可以执行以下操作:

import requests
from bs4 import BeautifulSoup
import urllib.request

for i in range(92):
    url = "https://www.paginegialle.it/lazio/roma/dentisti/p-"+str(i)+".html"
    r = requests.get(url)
    soup = BeautifulSoup(r.content, 'html.parser')
    p = soup.select('p')
    print(len(p))

enter image description here

关于python - 使用 Python 和 BeautifulSoup 抓取多个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60025078/

相关文章:

python - Flurry 登录 Requests.Session() Python 3

python - 解压文件导致 "BadZipFile: File is not a zip file"

python - 添加两个大数字(一百万位)的最快方法

python - 在 Pandas 中创建类似 Excel 的 SUMIFS

python - bs4 : Python 3 on mac 中未识别 lxml

python - 从 html <script> 中提取 JSON 对象

python - CRC-CCITT 16位Python手动计算

python - 如何找出Python正则表达式中组匹配的次数?

python - Beautiful Soup 无法从表中获取信息

python - 如何从 HTML 中删除 <li> 标签的内容