帮我写一段爬虫爬取网页内容的代码
时间: 2023-03-10 17:27:40 浏览: 143
Python爬取网页信息的示例
答案:
import requests
from bs4 import BeautifulSoupurl = 'http://example.com/'
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')# Find all the <p> tags
p_tags = soup.find_all('p')# Extract the text from each of the <p> tags and strip surrounding whitespace
for p_tag in p_tags:
print(p_tag.text.strip())
阅读全文