【发布时间】:2020-04-23 11:58:38
【问题描述】:
下面是我使用 BS4 进行抓取的 Python 代码。当我尝试运行循环时,它会打印相同的数据,请告诉我如何在 python 中运行分页循环。
import requests
from bs4 import BeautifulSoup as bs
url = 'https://www.yellowpages.com/los-angeles-ca/restaurants'
page = requests.get(url)
soup = bs(page.content,'html.parser')
#print(len(soup))
containers = soup.find_all("div",{"class","v-card"})
#print(containers[0])
name = containers.find_all("a",{"class","business-name"})
print(name[0].get_text())
phone = soup.find_all("div",{"class","phone"})
#print(phone[0].get_text())
add = soup.find_all("p",{"class","adr"})
#print(add[0].get_text())
for items in containers:
name_soup = containers.find("a",{"class","business-name"})
print(name_soup)
【问题讨论】:
-
请编辑您的问题并想一个更好的标题。还要正确格式化代码:stackoverflow.com/editing-help
-
这个标题会让你投下很多反对票。为什么不写一篇描述您的实际问题的文章?
标签: python python-3.x python-requests