Python 移动到Web Scraper中具有相同名称的下一个类
我的想法是建立一个市场刮板,在这里我可以找到我喜欢的东西,在这个特殊的例子中是盒式磁带,问题是当我Python 移动到Web Scraper中具有相同名称的下一个类,python,beautifulsoup,python-requests,Python,Beautifulsoup,Python Requests,我的想法是建立一个市场刮板,在这里我可以找到我喜欢的东西,在这个特殊的例子中是盒式磁带,问题是当我soup.find(attrs=“nameofclass”)时 打印值保持与顶部结果相同,我想查看页面上的所有产品 我尝试了一个while循环,但它始终返回相同的最大值 import requests from bs4 import BeautifulSoup import time URL = 'https://listado.mercadolibre.com.ar/cassette#D[A:c
soup.find(attrs=“nameofclass”)
时
打印值保持与顶部结果相同,我想查看页面上的所有产品
我尝试了一个while循环,但它始终返回相同的最大值
import requests
from bs4 import BeautifulSoup
import time
URL = 'https://listado.mercadolibre.com.ar/cassette#D[A:cassette]'
headers = {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36'}
page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
try:
while True:
title = soup.find(attrs={'main-title'})
price = soup.find(attrs=('price__fraction'))
print(title,price)
time.sleep(1)
except KeyboardInterrupt:
print('done')
尝试使用find_all方法 它应该返回该类中所有元素的列表。
然后您可以迭代该列表尝试使用find\u all方法 它应该返回该类中所有元素的列表。 然后您可以迭代该列表尝试使用CSS选择器 范例 尝试使用CSS选择器 范例
答案对你有用吗?答案对你有用吗?
import requests
from bs4 import BeautifulSoup
URL = 'https://listado.mercadolibre.com.ar/cassette#D[A:cassette]'
headers = {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/81.0.4044.113 Safari/537.36'}
page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.text, 'html.parser')
span = soup.select("span.main-title")
div = soup.select("div.price__container")
def get_title_and_price(span, div):
for i, item in enumerate(span):
title = item.getText()
price = div[i].getText()
print(title)
print(price)
get_title_and_price(span, div)