python - 使用 map 和 lambda 加入 BeautifulSoup Contents
问题描述
我想抓取网页内容并清理格式
from bs4 import BeautifulSoup
import urllib.request
import urllib.parse
import lxml
url='https://en.wikipedia.org/wiki/Deep_learning'
page=urllib.request.urlopen(url)
soup=BeautifulSoup(page,"lxml")
fetched_text=' '.join(map(lambda p: p.text.soup.find_all('p'),soup))
此代码最初是从这样的 youtube 教程中找到的:
fetched_text=' '.join(map(lambda p: p.text.soup.find_all('p')))
任何人都可以在这里帮助我吗?
解决方案
也许你使用列表理解更舒服:
fetched_text=' '.join([p.text for p in soup.find_all('p')])
或者你可以用 map 来做,记住 map 等待一个函数和一个迭代:
fetched_text=' '.join(map(lambda p: p.text, soup.find_all('p')))
推荐阅读
- python - 是否可以在 matplotlib 中的动画期间改变速度
- django - Django 实现自定义模型字段,无需迁移
- c++ - 结构内向量的初始化
- swift - 斯威夫特为什么通过防范 NIL 来展开可选的行不通
- performance - 当offline_user_session表有很多记录时keycloak启动很慢
- node.js - MQTT.js 一次将发送多少条消息?
- php - Laravel Nova indexQuery 范围重用
- javascript - 如何获取从快速服务器返回的错误的消息属性
- php - 使用 Headless Chrome,所有页面/站点的 PDF 上都没有呈现背景颜色
- python - Python ModuleNotFoundError:没有名为“xxx”的模块