使用 map 和 lambda 加入 BeautifulSoup 个内容
Join BeautifulSoup Contents with map and lambda
我想抓取网页内容并清理格式
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')))
但有人抱怨 map() 没有正确使用。
谁能帮我解决这个问题?
也许您更习惯使用列表理解:
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')))
我想抓取网页内容并清理格式
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')))
但有人抱怨 map() 没有正确使用。
谁能帮我解决这个问题?
也许您更习惯使用列表理解:
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')))