URL中有页码时,如何刮取多个页面?
例如:
https://www.cars.com/for-sale/searchresults.action/?mdId=21811&mkId=20024&page=**1**&perPage=100&rd=99999&searchSource=PAGINATION&showMore=false&sort=relevance&stkTypId=28880&zc=11209
我的代码:
import requests
from bs4 import BeautifulSoup
from csv import writer
response = requests.get('https://www.cars.com/for-sale/searchresults.action/?mdId=21811&mkId=20024&page=1&perPage=100&rd=99999&searchSource=PAGINATION&showMore=false&sort=relevance&stkTypId=28880&zc=11209')
soup = BeautifulSoup(response.text, 'html.parser')
posts = soup.find_all(class_='shop-srp-listings__inner')
with open('posts.csv', 'w') as csv_file:
csv_writer = writer(csv_file)
headers = ['title', 'color', 'price']
csv_writer.writerow(headers)
for post in posts:
title = post.find(class_="listing-row__title").get_text().replace('\n', '').strip()
# color = post.find("li").get_text().replace('\n', '')
price = post.find("span", attrs={"class": "listing-row__price"}).get_text().replace('\n', '').strip()
print(title, price)
# csv_writer.writerow([title, color, price])
谢谢你的帮助
最佳答案
page = 0
for x in range(25):
page+=1
url = ('https://www.cars.com/for-sale/searchresults.action/?mdId=21811&mkId=20024&page='+str(page)+
'&perPage=100&rd=99999&searchSource=PAGINATION&showMore=false&sort=relevance&stkTypId=28880&zc=11209')
print(url)
#requests.get(url)
关于python - 如何在URL中使用页码抓取多个页面,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55082926/