使用python进行网页抓取的问题

时间:2020-01-17 12:26:29

标签: python beautifulsoup python-requests

我要抓取页面,下面是我的代码:

import requests
from bs4 import BeautifulSoup
url = ('https://bama.ir/car/all-brands/all-models/all-trims?hasprice=true&sort=1&page1')

html_content = requests.get(url).text
soup = BeautifulSoup(html_content,'html.parser')
print(url)

names_car = []
prices = []
works = []

for name_car in soup.findAll('h2',{'itemprop':'name'}):
   names_car.append(name_car.text.replace('\n',' ').replace('\r',' ').strip())

for price in soup.findAll('p',{'class':'cost'}):
   prices.append(price.text.replace('\n',' ').replace('\r',' ').strip())

for work in soup.findAll('p',{'class': 'price milage-text-mobile visible-xs price-milage-mobile'}):
   works.append(work.text.replace('\n',' ').replace('\r',' ').strip())



for i in range(0,29):
   print({'name':names_car[i],'price':prices[i],'works':works[i]})

问题是:

  1. 当我以汽车,价格和工作的名字命名时,很难读懂

  2. 此代码仅适用于一页。我如何获得所有页面 和其他问题(当您运行代码时就可以理解)。

0 个答案:

没有答案