added crawlers session

This commit is contained in:
2021-10-05 16:54:28 +02:00
parent 238dc61ad6
commit 0b03819d40
17 changed files with 393 additions and 0 deletions
@@ -0,0 +1,26 @@
from bs4 import BeautifulSoup
import requests
url = "https://www.autoscout24.de/lst/bmw/45143-essen?sort=standard&desc=0&ustate=N%2CU&size=10&page=1&lon=6.96724&lat=51.46138&zip=45143%20Essen&zipr=100&cy=D&priceto=25000&fregfrom=2020&atype=C&fc=0&qry=&"
page = requests.get(url)
soup = BeautifulSoup(page.text, "html.parser")
print(soup.prettify())
title = soup.select_one("#cldt-ot-summary > div > h2")
print(title.text)
prices = soup.select('.cldt-summary-vehicle-data ul[data-item-name="vehicle-details"] li[data-type="mileage"]')
types = soup.select('.cldt-summary-vehicle-data ul[data-item-name="vehicle-details"] li.summary_item_no_bottom_line')
car_names = soup.select(".cldt-summary-makemodel")
print(car_names)
for name in car_names:
print(name.text)
for price in prices:
print(price.text)
for type in types:
print(type.text)
@@ -0,0 +1,23 @@
import requests
host = "www.autoscout24.de"
path = "/lst/bmw"
location = "/45143-essen"
sort_art = 1 # descending
per_page = 10 # items per page
page_num = 2 # page number
price = 10000 # price
year = 2019 # year
query_string = f"?sort=standard&desc={sort_art}&ustate=N%2CU&size={per_page}&page={page_num}&lon=6.96724&lat=51.46138&zip=45143%20Essen&zipr=100&cy=D&priceto={price}&fregfrom={year}&atype=C"
start_url = f"https://{host}{path}{location}{query_string}"
downloaded_page = requests.get(start_url)
page_html = downloaded_page.text
print(page_html)
+69
View File
@@ -0,0 +1,69 @@
[
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA5KF396811/2019-tesla-model-3/?zipcode=77477", "model": "2019 Tesla Model 3", "color": "Blue", "milage": "10,594", "price": "$41,499"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA7JF047261/2018-tesla-model-3/?sponsoredVehiclePosition=1", "model": "2018 Tesla Model 3", "color": "Black", "milage": "37,554", "price": "$43,999"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EBXKF199321/2019-tesla-model-3/?sponsoredVehiclePosition=2", "model": "2019 Tesla Model 3", "color": "Blue", "milage": "25,683", "price": "$49,300"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA3JF051534/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "White", "milage": "17,500", "price": "$45,900"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA8KF361650/2019-tesla-model-3/", "model": "2019 Tesla Model 3", "color": "Black", "milage": "46,334", "price": "$39,980"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA2JF036409/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Black", "milage": "33,241", "price": "$44,893"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA3JF009249/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "White", "milage": "43,179", "price": "$43,999"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA0KF324706/2019-tesla-model-3/", "model": "2019 Tesla Model 3", "color": "Black", "milage": "50,000", "price": "$39,000"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA8JF123716/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Silver", "milage": "27,849", "price": "$44,500"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA4JF042129/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Black", "milage": "20,833", "price": "$41,999"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA8JF156456/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Black", "milage": "52,864", "price": "$43,395"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA6JF077643/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Silver", "milage": "53,839", "price": "$39,242"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA3JF035088/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "White", "milage": "19,297", "price": "$46,300"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EAXJF038621/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Blue", "milage": "31,902", "price": "$44,777"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA9JF118315/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Blue", "milage": "28,100", "price": "$42,500"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA8JF177856/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Black", "milage": "47,891", "price": "$40,940"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA0KF296695/2019-tesla-model-3/", "model": "2019 Tesla Model 3", "color": "Black", "milage": "18,195", "price": "$44,880"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA9JF035709/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Black", "milage": "52,000", "price": "$41,900"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA8JF171586/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Black", "milage": "36,011", "price": "$40,941"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA0JF123113/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Black", "milage": "53,042", "price": "$44,989"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA4JF007025/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Silver", "milage": "42,492", "price": "$44,989"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA2JF015740/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "White", "milage": "30,875", "price": "$42,498"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA4KF190413/2019-tesla-model-3/", "model": "2019 Tesla Model 3", "color": "Blue", "milage": "21,219", "price": "$43,730"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA2JF154122/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Blue", "milage": "28,023", "price": "$38,000"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA7JF038883/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "White", "milage": "29,760", "price": "$45,000"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EAXJF006137/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Silver", "milage": "37,000", "price": "$43,000"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EB8JF119027/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Black", "milage": "90,586", "price": "$39,989"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA9JF011619/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Silver", "milage": "49,381", "price": "$45,989"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA3JF158549/2018-tesla-model-3/?zipcode=76131", "model": "2018 Tesla Model 3", "color": "Gray", "milage": "44,392", "price": "$41,990"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EB9KF450569/2019-tesla-model-3/", "model": "2019 Tesla Model 3", "color": "Black", "milage": "31,020", "price": "$50,500"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EAXJF189958/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Blue", "milage": "11,000", "price": "$41,500"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA9JF023916/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Gray", "milage": "35,831", "price": "$44,500"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EAXJF163019/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Gray", "milage": "26,577", "price": "$45,400"}
][
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EB9JF054091/2018-tesla-model-3/?zipcode=20166", "model": "2018 Tesla Model 3", "color": "Black", "milage": "16,055", "price": "$49,499"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA8MF922013/2021-tesla-model-3/?zipcode=76131", "model": "2021 Tesla Model 3", "color": "White", "milage": "2,752", "price": "$47,990"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA7JF047261/2018-tesla-model-3/?sponsoredVehiclePosition=2", "model": "2018 Tesla Model 3", "color": "Black", "milage": "37,554", "price": "$43,499"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA8KF361650/2019-tesla-model-3/", "model": "2019 Tesla Model 3", "color": "Black", "milage": "46,334", "price": "$39,980"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA3JF009249/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "White", "milage": "43,179", "price": "$43,999"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA0KF324706/2019-tesla-model-3/", "model": "2019 Tesla Model 3", "color": "Black", "milage": "50,000", "price": "$39,000"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA3JF051534/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "White", "milage": "17,500", "price": "$45,900"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA9JF035709/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Black", "milage": "54,000", "price": "$41,300"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA2JF036409/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Black", "milage": "33,241", "price": "$44,893"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA4JF042129/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Black", "milage": "20,833", "price": "$41,999"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA7JF038883/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "White", "milage": "29,760", "price": "$43,980"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA9JF118315/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Blue", "milage": "28,100", "price": "$42,500"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA4KF190413/2019-tesla-model-3/", "model": "2019 Tesla Model 3", "color": "Blue", "milage": "21,219", "price": "$43,730"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA8JF123716/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Silver", "milage": "27,849", "price": "$44,500"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA3JF035088/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "White", "milage": "19,297", "price": "$46,300"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA2JF015740/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "White", "milage": "30,875", "price": "$42,498"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA8JF156456/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Black", "milage": "52,864", "price": "$43,395"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EAXJF038621/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Blue", "milage": "31,902", "price": "$44,777"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EAXJF163019/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Gray", "milage": "19,854", "price": "$45,400"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EB9KF450569/2019-tesla-model-3/", "model": "2019 Tesla Model 3", "color": "Black", "milage": "31,020", "price": "$50,500"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EAXJF006137/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Silver", "milage": "37,000", "price": "$43,000"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA2JF154122/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Blue", "milage": "28,023", "price": "$38,000"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA0KF296695/2019-tesla-model-3/", "model": "2019 Tesla Model 3", "color": "Black", "milage": "18,195", "price": "$44,880"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA0JF123113/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Black", "milage": "53,147", "price": "$44,989"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA9JF164873/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "White", "milage": "15,581", "price": "$41,500"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA8JF171586/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Black", "milage": "36,011", "price": "$40,941"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA8JF177856/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Black", "milage": "47,891", "price": "$40,941"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA3JF158549/2018-tesla-model-3/?zipcode=76131", "model": "2018 Tesla Model 3", "color": "Gray", "milage": "44,392", "price": "$41,990"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA9JF023916/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Gray", "milage": "35,831", "price": "$44,500"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA5KF411436/2019-tesla-model-3/", "model": "2019 Tesla Model 3", "color": "Red", "milage": "25,578", "price": "$41,499"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA9KF431219/2019-tesla-model-3/", "model": "2019 Tesla Model 3", "color": "White", "milage": "24,915", "price": "$42,991"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EA7KF410336/2019-tesla-model-3/", "model": "2019 Tesla Model 3", "color": "Black", "milage": "57,500", "price": "$37,000"},
{"url": "http://truecar.com/used-cars-for-sale/listing/5YJ3E1EB8JF119027/2018-tesla-model-3/", "model": "2018 Tesla Model 3", "color": "Black", "milage": "90,586", "price": "$39,989"}
]
+11
View File
@@ -0,0 +1,11 @@
# Automatically created by: scrapy startproject
#
# For more information about the [deploy] section see:
# https://scrapyd.readthedocs.io/en/latest/deploy.html
[settings]
default = truecar.settings
[deploy]
#url = http://localhost:6800/
project = truecar
+12
View File
@@ -0,0 +1,12 @@
# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html
import scrapy
class TruecarItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
pass
+103
View File
@@ -0,0 +1,103 @@
# Define here the models for your spider middleware
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/spider-middleware.html
from scrapy import signals
# useful for handling different item types with a single interface
from itemadapter import is_item, ItemAdapter
class TruecarSpiderMiddleware:
# Not all methods need to be defined. If a method is not defined,
# scrapy acts as if the spider middleware does not modify the
# passed objects.
@classmethod
def from_crawler(cls, crawler):
# This method is used by Scrapy to create your spiders.
s = cls()
crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
return s
def process_spider_input(self, response, spider):
# Called for each response that goes through the spider
# middleware and into the spider.
# Should return None or raise an exception.
return None
def process_spider_output(self, response, result, spider):
# Called with the results returned from the Spider, after
# it has processed the response.
# Must return an iterable of Request, or item objects.
for i in result:
yield i
def process_spider_exception(self, response, exception, spider):
# Called when a spider or process_spider_input() method
# (from other spider middleware) raises an exception.
# Should return either None or an iterable of Request or item objects.
pass
def process_start_requests(self, start_requests, spider):
# Called with the start requests of the spider, and works
# similarly to the process_spider_output() method, except
# that it doesn’t have a response associated.
# Must return only requests (not items).
for r in start_requests:
yield r
def spider_opened(self, spider):
spider.logger.info('Spider opened: %s' % spider.name)
class TruecarDownloaderMiddleware:
# Not all methods need to be defined. If a method is not defined,
# scrapy acts as if the downloader middleware does not modify the
# passed objects.
@classmethod
def from_crawler(cls, crawler):
# This method is used by Scrapy to create your spiders.
s = cls()
crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
return s
def process_request(self, request, spider):
# Called for each request that goes through the downloader
# middleware.
# Must either:
# - return None: continue processing this request
# - or return a Response object
# - or return a Request object
# - or raise IgnoreRequest: process_exception() methods of
# installed downloader middleware will be called
return None
def process_response(self, request, response, spider):
# Called with the response returned from the downloader.
# Must either;
# - return a Response object
# - return a Request object
# - or raise IgnoreRequest
return response
def process_exception(self, request, exception, spider):
# Called when a download handler or a process_request()
# (from other downloader middleware) raises an exception.
# Must either:
# - return None: continue processing this exception
# - return a Response object: stops process_exception() chain
# - return a Request object: stops process_exception() chain
pass
def spider_opened(self, spider):
spider.logger.info('Spider opened: %s' % spider.name)
+13
View File
@@ -0,0 +1,13 @@
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
class TruecarPipeline:
def process_item(self, item, spider):
return item
+88
View File
@@ -0,0 +1,88 @@
# Scrapy settings for truecar project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
# https://docs.scrapy.org/en/latest/topics/settings.html
# https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
# https://docs.scrapy.org/en/latest/topics/spider-middleware.html
BOT_NAME = 'truecar'
SPIDER_MODULES = ['truecar.spiders']
NEWSPIDER_MODULE = 'truecar.spiders'
# Crawl responsibly by identifying yourself (and your website) on the user-agent
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36'
# Obey robots.txt rules
ROBOTSTXT_OBEY = False
# Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32
# Configure a delay for requests for the same website (default: 0)
# See https://docs.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
#DOWNLOAD_DELAY = 3
# The download delay setting will honor only one of:
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
#CONCURRENT_REQUESTS_PER_IP = 16
# Disable cookies (enabled by default)
#COOKIES_ENABLED = False
# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False
# Override the default request headers:
#DEFAULT_REQUEST_HEADERS = {
# 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
# 'Accept-Language': 'en',
#}
# Enable or disable spider middlewares
# See https://docs.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
# 'truecar.middlewares.TruecarSpiderMiddleware': 543,
#}
# Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
# 'truecar.middlewares.TruecarDownloaderMiddleware': 543,
#}
# Enable or disable extensions
# See https://docs.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
# 'scrapy.extensions.telnet.TelnetConsole': None,
#}
# Configure item pipelines
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
#ITEM_PIPELINES = {
# 'truecar.pipelines.TruecarPipeline': 300,
#}
# Enable and configure the AutoThrottle extension (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False
# Enable and configure HTTP caching (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'
@@ -0,0 +1,4 @@
# This package will contain the spiders of your Scrapy project
#
# Please refer to the documentation for information on how to create and manage
# your spiders.
@@ -0,0 +1,23 @@
import scrapy
class TruecarSpider(scrapy.Spider):
name = "truecar_crawler"
def start_requests(self):
url = 'https://www.truecar.com/used-cars-for-sale/listings/tesla/model-3/'
yield scrapy.Request(url=url, callback=self.parse)
def parse(self, response):
cars = response.xpath('//div[@data-test="allVehicleListings"]/ul/li')
for car in cars:
make_model = car.css('h3[data-test="vehicleListingCardTitle"] > div')
year = make_model.css('span.vehicle-card-year::text').get()
model_raw = make_model.css('span.vehicle-header-make-model::text').getall()
#model = model_raw[model_raw.find('>') + 1:-7].replace('<!-- -->','')
tesla_data = {
'url': f'http://truecar.com{car.css("a::attr(href)").get()}',
'model': f'{year} {model_raw[0]} {model_raw[2]}',
'color': car.css('div[data-test="cardContent"] > div > div.text-truncate::text').get(),
'milage': car.css('div[data-test="vehicleMileage"]::text').get(),
'price': car.css('div[data-test="vehicleListingPriceAmount"] > div.heading-3[data-test="vehicleCardPricingBlockPrice"]::text').get()
}
yield tesla_data
+21
View File
@@ -0,0 +1,21 @@
from selenium.webdriver import Chrome
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
import time
url = 'https://www.iseecars.com/cars-for-sale#Location=90011&Radius=50&Make=Tesla&Model=Model+3&_t=a&maxResults=20&sort=BestDeal&sortOrder=desc&lfc_t0=MTYzMTAwMjc0MzQ4MQ%3D%3D'
chrome_options = Options()
chrome_options.add_argument("--headless")
with Chrome(options = chrome_options) as driver:
wait = WebDriverWait(driver, 10)
driver.get(url)
time.sleep(10)
teslas = driver.find_element_by_css_selector('div#cars_v2-result-list article')
model = teslas.find_element_by_css_selector('h3')
print(model.text)