我尝试使用 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))
关于python - 使用 Python 和 BeautifulSoup 抓取多个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60025078/