Python 如何遍历HTML文件中的元素
这是我正在查看的页面: 我已导入BeautifulSoup和请求。我想创建一个包含此页面所有标题的文本文件。我可以用它来买一个Python 如何遍历HTML文件中的元素,python,html,web-scraping,Python,Html,Web Scraping,这是我正在查看的页面: 我已导入BeautifulSoup和请求。我想创建一个包含此页面所有标题的文本文件。我可以用它来买一个 from bs4 import BeautifulSoup import requests source = requests.get('https://www.nytimes.com/topic/destination/russia').text soup = BeautifulSoup(source, 'lxml') headline = soup.find('h2
from bs4 import BeautifulSoup
import requests
source = requests.get('https://www.nytimes.com/topic/destination/russia').text
soup = BeautifulSoup(source, 'lxml')
headline = soup.find('h2').get_text()
print(headline)
这将产生:
When an Oil Price War Meets Coronavirus Fears, Markets Get Punched in the Face
一切都好。然而,我完全不知道如何反复浏览和收集页面上的所有标题。任何帮助都将不胜感激。请尝试:
for headline in soup.find_all('h2'):
print(healdine.get_text())
find_all
将所有
标记作为列表返回。现在反复浏览它。尝试:
for headline in soup.find_all('h2'):
print(healdine.get_text())
find_all
将所有
标记作为列表返回。现在反复阅读。使用find_all()
获取所有标题
使用for
循环从每个循环中获取文本并打印
from bs4 import BeautifulSoup
import requests
source = requests.get('https://www.nytimes.com/topic/destination/russia').text
soup = BeautifulSoup(source)
headings = soup.find_all('h2')
for h in headings:
heading = h.get_text()
print(heading)
使用find_all()
获取所有标题
使用for
循环从每个循环中获取文本并打印
from bs4 import BeautifulSoup
import requests
source = requests.get('https://www.nytimes.com/topic/destination/russia').text
soup = BeautifulSoup(source)
headings = soup.find_all('h2')
for h in headings:
heading = h.get_text()
print(heading)
这回答了你的问题吗?你能澄清到底是什么问题吗?听起来你只需要学习如何使用BeautifulSoup。我的回答对你有帮助吗?如果是这样,别忘了点击我答案旁边的勾号:)这是否回答了你的问题?你能澄清到底是什么问题吗?听起来你只需要学习如何使用BeautifulSoup。我的回答对你有帮助吗?如果是这样,别忘了点击我答案旁边的勾号:)